如何减少从模拟摇杆输入转换的旋转 "jumpy"

How to make rotation translated from analog stick input less "jumpy"

我正在制作一个游戏(在 Unity 中),其中角色有一个围绕它旋转的指针。指针定义角色移动的方向。

实际上,指针移动的方式很有"jumpy"的感觉。我想让它更多 natural/smoother 但我真的不知道如何解决这个问题。

以下是我如何围绕播放器旋转指针:

        float x = Input.GetAxis("Horizontal"),
            y = Input.GetAxis("Vertical");

        if (x != 0 || y != 0) 
        {
            toRotate.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(y, x) * Mathf.Rad2Deg));
        }

我在 Update() 函数中执行此操作。

toRotate 是一个 Transform

正如 Sichi 指出的那样,执行此操作的方法是在旋转时使用 Quaternion.LerpQuaternion.Slerp

这个效果很好。

    float x = Input.GetAxis("Horizontal")
    float y = Input.GetAxis("Vertical");
    float lerpAmount= 10;

    if (x != 0 || y != 0) 
    {
        toRotate.rotation = Quaternion.Lerp(
            toRotate.rotation,
            Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(y, x) * Mathf.Rad2Deg),
            lerpAmount * Time.deltaTime);
    }

您也可以使用Quarternion.RotateTowards

float x = Input.GetAxis("Horizontal")
float y = Input.GetAxis("Vertical");
float stepSize = 10 * Time.deltaTime;

if (x != 0 || y != 0) 
{
    var currentRotation = toRotate.rotation;
    var targetEuler = new Vector3(0, 0, Mathf.Atan2(y, x) * Mathf.Rad2Deg);
    var targetRotation = Quarternion.Euler(targetEuler);
    toRotate.rotation = Quarternion.RotateTowards(currentRotation, targetRotation, stepSize);
}