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



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

事实上,指针移动的方式感觉非常"跳跃"。我想让它更自然/更流畅,但我真的不知道如何处理这个问题。

以下是我围绕播放器旋转指针的方法:

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);
}

最新更新