【发布时间】:2018-07-27 01:26:07
【问题描述】:
所以我遇到了这个问题,我试图让子弹在 Unity 中跳出障碍物。目前我正在使用 Vector3.Reflect 来设置我的对象的 Vector3.Forward。
Expected Result vs. Actual Result
我的预期结果是项目符号遵循红线。它反弹的每个物体都旋转了 45 度,因此子弹应该遵循完美的 90 度反弹。但实际发生的情况是在第二次弹跳时,子弹沿着蓝线移动,从那一点卡在角落里。
public int bulletSpeed;
Rigidbody bulletRigidBody;
void Start()
{
//Get bullet rigidbody
if (bulletRigidBody == null)
{
bulletRigidBody = GetComponent<Rigidbody>();
}
bulletRigidBody.velocity = transform.forward * bulletSpeed;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Wall")
{
//change forward vector based on reflection against object
transform.forward = Vector3.Reflect(collision.contacts[0].point, collision.contacts[0].normal);
//add velocity to bullet based on new forward vector
bulletRigidBody.velocity = transform.forward * bulletSpeed;
}
}
我假设我的问题与返回不正确的法线有关,或者因为我设置的是正向矢量而不是旋转,但我不确定如何将 Vector3.Reflect 转换为四元数可以理解的东西
【问题讨论】:
-
你不应该编辑你的问题来添加你的答案,而是将你的答案添加为答案。
标签: c# unity3d physics game-physics