Unity3d Quaternion LookRotation 没有返回正确的值

Unity3d Quaternion LookRotation not returning proper value

我想要一个射弹来注视目标对象,为此我使用如下所示的四元数 LookRotation

targetRotation = Quaternion.LookRotation(targetPosition - projectile.transform.position);
if(targetRotation.eulerAngles.magnitude <= 60)
  projectile.transform.rotation = targetRotation;

我在这里设置了 if 条件,使它更真实地转向目标,否则如果它的转向超过 60 度,弹丸就不会转向。

现在如下图所示,我们可以看到目标对象的角度不超过 60 度,但在调试时我仍然得到 328 作为 targetRotation.eulerAngles.magnitude,这是获取 if 条件失败并且弹丸没有朝向目标对象旋转。

Quaternion.LookRotation(targetPosition - projectile.transform.position) 表示 "Give me a quaternion that represents a rotation of a vector from up towards targetPosition - projectile.transform.position"。 eulerAngles 只是旋转的另一种表示,您不会从它的幅度中得到任何有意义的东西。

我怀疑你不想要那个。相反,我怀疑您想知道 弹丸是否需要从其当前前进方向 转动超过 60 度 在那种情况下,您可能想检查射弹的前向矢量与其朝向目标的方向矢量之间的角度。

我没有打开 Unity,所以我不知道这是否可以编译,但它应该是这样的:

var directionToTarget = targetPosition - projectile.transform.position;
var angleToTarget = Vector3.Angle(projectile.transform.forward, directionToTarget);
if (angleToTarget < 60) ...

你提到你想要更多 "realistic" 转弯。如果超过60度,你想让弹丸做什么?