【问题标题】:Quaternion Lerp not giving good results with LookRotation四元数 Lerp 使用 LookRotation 没有给出好的结果
【发布时间】:2018-10-19 00:52:08
【问题描述】:

我正在尝试平滑地翻转我的第一人称角色,同时能够使用鼠标外观在 y 轴上旋转玩家。问题是 lerp 并没有以确切的结果结束。我希望最终的旋转结果是精确的,使得 (transform.up == TargetUpDirection())

void FixOrientation() {
    if(transform.up != TargetUpDirection()) //Vector3(0, -1, 0)
    {
        transform.rotation = 
            Quaternion.Lerp(transform.rotation,
                            Quaternion.LookRotation(transform.forward,
                            TargetUpDirection(), 0.1f);
    }
}

如果我在翻转时使用鼠标查看,transform.up.y 的结果总是小于 1,例如 0.99881231,我需要继续使用鼠标查看,直到它自行纠正并且我不喜欢它。

原因可能是当我使用鼠标查看时,字符向前被修改,这导致了这种情况,但我不知道如何实现解决方案,因为四元数让我感到困惑。

【问题讨论】:

  • Quaternion.Lerp 需要 3 个参数。
  • 对不起,我编辑了它以添加第三个
  • 我会尝试使用 slerp 而不是 lerp,但我认为实际问题可能是您的 mouselook 功能中的万向节锁定。
  • 肯定用Slerp,但万向节锁不是问题,因为他使用的是四元数。
  • 我认为您在TargetUpDirection() 之后缺少)

标签: c# unity3d


【解决方案1】:

这是因为正如所写,Lerp 总是让你旋转 10% 的剩余路程。这意味着当您接近目标旋转时,您的旋转量将减少。最终,它会减小到在您实际到达目的地之前,舍入误差使您旋转 0 度的程度。

在大多数情况下,您不想使用t 的常量值调用Lerp

这是另一种选择。首先,选择一个最大转速:

float smooth = 5f;

然后,使用Quaternion.RotateTowards:

Quaternion targetRotation = Quaternion.LookRotation(transform.forward, TargetUpDirection());
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation , smooth * Time.deltaTime);

此答案基于 Unity 论坛上的 this post

【讨论】:

  • 我的解决方案。我只是在变换方向时锁定了我的鼠标外观,并在向上对齐时解锁它。不是最好的解决方案,但它可以工作
猜你喜欢
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2020-11-22
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
相关资源
最近更新 更多