如何移动3D射弹Unity

问题描述 投票:0回答:2

我试图了解在 Unity 中创建 3D 射弹的过程。在有关创建常规激光弹丸的少数在线帖子中,关于该过程的解释很少。有人可以帮助我了解如何拍摄射弹的分步方法吗?

我想理解的问题:

  • 如何将弹丸移动到射击者游戏对象所面对的方向。使用position.Translate()方法迫使我选择一个特定的方向来移动对象。
c# unity-game-engine game-physics projectile
2个回答
3
投票

如何将弹丸朝射击者的方向移动 游戏对象面向

您使用相机的

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; }
    

-1
投票
感谢您对此的帮助。除非我向任一方向转身超过 90 度,否则这种方法有效。当我实例化一个面向 -Z 空间的新子弹时,子弹在创建后立即消失?我猜这是我的错误?

© www.soinside.com 2019 - 2024. All rights reserved.