【问题标题】:Unity3d Quaternion LookRotation not returning proper valueUnity3d四元数LookRotation没有返回正确的值
【发布时间】:2019-02-13 18:19:33
【问题描述】:

我想要一个射弹来查看目标对象,为此我使用四元数 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 条件失败且弹丸未向目标对象旋转。

【问题讨论】:

    标签: c# unity3d quaternions euler-angles projectile


    【解决方案1】:

    Quaternion.LookRotation(targetPosition - projectile.transform.position) 的意思是“给我一个四元数,它代表一个向量从上targetPosition - projectile.transform.position 的旋转”。 eulerAngles 只是旋转的另一种表示,你不会从它的大小中得到任何有意义的东西。

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

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

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

    你提到你想要一个更“现实”的转弯。如果超过 60 度,你想让弹丸做什么?

    【讨论】:

    • 太酷了!让我试试这个,对于你的答案,如果它超过 60 度,我让弹丸朝着它的最后一个转弯方向移动。
    猜你喜欢
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多