Hol*_*den 1 c# unity-game-engine
我正试图制作一个像Unity一样旋转的跷跷板,由于某种原因旋转是瞬间的,而不是一个渐进的运动.我之前曾调查过Slerp和Lerp,但我也无法使用它.我想知道是否有人有任何见解,因为我确信我错过了一些愚蠢而且简单的xD.谢谢!这是方法.
private void Rotate(float rotateAmount)
{
var oldRotation = transform.rotation;
transform.Rotate(0, 0, rotateAmount);
var newRotation = transform.rotation;
for (float t = 0; t <= 1.0; t += Time.deltaTime)
{
transform.rotation = Quaternion.Slerp(oldRotation, newRotation, t);
}
transform.rotation = newRotation;
}
}Run Code Online (Sandbox Code Playgroud)
for循环中完成的所有旋转都发生在一帧之下.你不能看到他们在一帧下改变.使Rotate函数成为一个协程函数然后在每个循环中等待一个帧yield return null,你应该看到随时间的变化.
private IEnumerator Rotate(float rotateAmount)
{
var oldRotation = transform.rotation;
transform.Rotate(0, 0, rotateAmount);
var newRotation = transform.rotation;
for (float t = 0; t <= 1.0; t += Time.deltaTime)
{
transform.rotation = Quaternion.Slerp(oldRotation, newRotation, t);
yield return null;
}
transform.rotation = newRotation;
}
Run Code Online (Sandbox Code Playgroud)
因为它是一个协同程序函数,所以你可以启动它而不是直接调用它:
StartCoroutine(Rotate(90f));
Run Code Online (Sandbox Code Playgroud)
我注意到你曾经transform.Rotate(0, 0, rotateAmount)得到目的地角度.你不需要这样做.如果需要将对象从当前角度旋转到另一个角度,请获取当前角度,然后将其添加到目标角度,并使用它来Vector3.Lerp旋转旋转.
参见"增量角度旋转OVER TIME:"部分,从这个职位.
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |