我现在有一个设置,其中我有一个 3D 对象,该对象内部有一个空对象。当您按下一个按钮时,相机跟随的空物体会旋转 90 度。如果您按下另一个按钮,它将向另一个方向旋转 90 度。结果是相机可以在完成旋转之前围绕对象旋转 4 次。
目前它运作良好,但我试图弄清楚如何为动画添加一些缓动,使其看起来不那么粗糙。我对在动画中使用曲线有一点了解,但我不确定如何将其应用于代码(或者它是否是最好的做事方式)
public InputMaster controls;
private bool isSpinning;
private float rotationSpeed = 0.3f;
IEnumerator RotateMe(Vector3 byangles, float intime)
{
var fromangle = transform.rotation;
var toangle = Quaternion.Euler(transform.eulerAngles + byangles);
for (var t = 0f; t < 1; t += Time.deltaTime / intime)
{
transform.rotation = Quaternion.Slerp(fromangle, toangle, t);
yield return null;
transform.rotation = toangle;
}
Debug.Log("finished rotation");
isSpinning = false;
Debug.Log("isSpinning now false");
}
上面的代码是我创建一个协程的地方,它说明了它将如何转换。让我有点困惑的一件事是,如果我没有说 的线,transform.rotation = toangle;旋转就会出现在 89.5 度之类的地方,如果你多次这样做,它会偏离几度。不知道为什么会这样。
void Rotation(float amount)
{
Debug.Log("rotation number is " + amount);
float holdingRotate = controls.Player.Camera.ReadValue<float>();
if (holdingRotate == 1 && isSpinning == false)
{
isSpinning = true;
Debug.Log("isSpinning now true");
StartCoroutine(RotateMe(Vector3.up * 90, rotationSpeed));
}
else if (holdingRotate == -1 && isSpinning == false)
{
isSpinning = true;
Debug.Log("isSpinning now true");
StartCoroutine(RotateMe(Vector3.up * -90, rotationSpeed));
}
}
这部分是调用动画的地方。非常感谢任何帮助。
