0
\$\begingroup\$

The rotation is very fast. It takes less than a second and for sure not 3 seconds.

Maybe it's better to do it without using a coroutine? Or something in the IEnumerator is wrong with the time and progress calculation?

public IEnumerator DoRotate(float time)
{
    var newRotation = Quaternion.AngleAxis(180, Vector3.up) * transform.rotation;

    var progress = 0f;

    while (progress < 1)
    {
        progress += Time.deltaTime / time;

        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, progress);

        yield return new WaitForEndOfFrame();
    }
}

Usage :

private void FixedUpdate()
{
    if (rotateBackwards)
    {
        StartCoroutine(DoRotate(3f));
        rotateBackwards = false;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You forgot to cache your starting rotation, and rotate each frame relative to that starting point, rather than where you already advanced to:

public IEnumerator DoRotate(float time)
{
    var startRotation = transform.rotation;

    var newRotation = Quaternion.AngleAxis(180, Vector3.up) * transform.rotation;

    var progress = 0f;

    while (progress < 1)
    {
        progress += Time.deltaTime / time;

        transform.rotation = Quaternion.Slerp(startRotation, newRotation, progress);

        yield return null;
    }

    transform.rotation = newRotation;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .