【发布时间】:2019-04-23 16:16:02
【问题描述】:
我正在尝试创建一个枪支脚本,但是在子弹预制件实例化后,它并没有朝着正确的方向(直线)行进。用于在 Shoot() 中创建子弹的函数,当 Update 循环从 GetMouseButton(0) 获取输入时调用该函数。
public class CharController : MonoBehaviour {
[SerializeField]
float moveSpeed = 4f;
public float aimSpeed;
Vector3 mousePos;
Vector3 forward, right;
public GameObject bulletSpawnPoint;
public GameObject bullet;
public float bullet_Speed;
public float fireRate;
void Start () {
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}
void Update () {
if (Input.GetMouseButton(0))
{
transform.position += (-transform.position + mousePos).normalized * aimSpeed * Time.deltaTime;
transform.position = new Vector3(transform.position.x, 2.5f, transform.position.z);
Shoot();
}
}
void Shoot()
{
GameObject temp_Bullet_Handler;
temp_Bullet_Handler = Instantiate(bullet, bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.rotation) as GameObject;
//temp_Bullet_Handler.transform.Rotate(Vector3.left * 90);
Rigidbody temp_Rigidbody;
temp_Rigidbody = temp_Bullet_Handler.GetComponent<Rigidbody>();
temp_Rigidbody.AddForce(Vector3.forward * Time.deltaTime * 10f);
Destroy(temp_Bullet_Handler, 10.0f);
}
}
如果有人对导致子弹从实例化点不沿直线方向行进的原因有任何信息,我很想听听您的意见。
提前致谢
【问题讨论】:
标签: unity3d