我试图了解在 Unity 中创建 3D 射弹的过程。在有关创建常规激光弹丸的少数在线帖子中,关于该过程的解释很少。有人可以帮助我了解如何拍摄射弹的分步方法吗?
我想理解的问题:
如何将弹丸朝射击者的方向移动 游戏对象面向
您使用相机的
Transform.forward
使射弹向玩家面对的位置移动。
发射弹丸的过程如下:
1.实例化/创建项目符号
2.设置子弹在玩家面前的位置
3。获取附加到该实例化项目符号的
Rigidbody
4。 如果这只是带有角色控制器的相机并且没有可见的枪,
使用
Camera.main.Transform.Position.forward
+ shootSpeed
变量射击子弹。
如果您想要使用可见的枪或物体进行射击,
创建另一个游戏对象(ShootingTipPoint),将其用作子弹射击的位置,并将其放置在您想要射击的枪或对象的位置,然后使用该游戏对象的ShootingTipPoint.Transform.Position.forward
而不是
Camara.Main.Transform.Position.forward
来射击子弹.还有一个工作代码:
public GameObject bulletPrefab;
public float shootSpeed = 300;
Transform cameraTransform;
void Start()
{
cameraTransform = Camera.main.transform;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
shootBullet();
}
}
void shootBullet()
{
GameObject tempObj;
//Instantiate/Create Bullet
tempObj = Instantiate(bulletPrefab) as GameObject;
//Set position of the bullet in front of the player
tempObj.transform.position = transform.position + cameraTransform.forward;
//Get the Rigidbody that is attached to that instantiated bullet
Rigidbody projectile = GetComponent<Rigidbody>();
//Shoot the Bullet
projectile.velocity = cameraTransform.forward * shootSpeed;
}