csharp Unity C#片段用于将变换旋转到新的相对旋转数秒

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Unity C#片段用于将变换旋转到新的相对旋转数秒相关的知识,希望对你有一定的参考价值。

// Rotate the object from it's current rotation to "newRotation" over "duration" seconds
void StartRotate(Vector3 newRotation, float duration = 0.5f)
{
    if (SlerpRotation != null) // if the rotate coroutine is currently running, so let's stop it and begin rotating to the new rotation.
        StopCoroutine(SlerpRotation);

    SlerpRotation = Rotate(newRotation, duration);
    StartCoroutine(SlerpRotation);
}

IEnumerator SlerpRotation = null;
bool done = false;

IEnumerator Rotate(Vector3 newRotation, float duration)
{
    Quaternion startRotation = transform.rotation; // You need to cache the current rotation so that you aren't slerping from the updated rotation each time
    Quaternion endRotation = Quaternion.Euler(newRotation);

    for (float elapsed = 0f; elapsed < duration; elapsed += Time.deltaTime)
    {
        float t = elapsed / duration; // This is the normalized time. It will move from 0 to 1 over the duration of the loop.
        transform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
        yield return null;
  }

    done = false;

    transform.rotation = endRotation; // finally, set the rotation to the end rotation
    SlerpRotation = null; // Clear out the IEnumerator variable so we can tell that the coroutine has ended.
}

以上是关于csharp Unity C#片段用于将变换旋转到新的相对旋转数秒的主要内容,如果未能解决你的问题,请参考以下文章

csharp 团结/变换/旋转

unity3d的四元数 Quaternion

Unity四元数和旋转

Unity Rigidbody2D 手册

unity3d中的矩阵有啥用

unity 有插值怎么解决摄像机的抖动