How to rotate an object smoothly to 0, 0, 0?

I have an object that I need to reset the rotation of smoothly, using

transform.rotation = Quaternion.Euler(0f, 0f, 0f);

will set the rotation instantly which looks bad. Is there a simple way move the rotation back to these values?

transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0f, 0f, 0f), 1.0f * Time.deltaTime);

?

No sorry it isnt working for me. It is either doing nothing or setting to 0 instantly depending on whether it is attached directly to the object or being referenced from another object.

Quaternion.RotateTowards? Use Quaternion.identity as the target rotation.

2 Likes

try changing the 1.0f * Time.DeltaTime into 0.1f

1 Like

Using a coroutine and slerp:

//start it wherever you decide to start the animation. On key press, on trigger enter, on whatever.
//in this example I'm rotating 'this', towards (0,0,0), for 1 second
StartCoroutine(AnimateRotationTowards(this.transform, Quaternion.identity, 1f));



private System.Collections.IEnumerator AnimateRotationTowards(Transform target, Quaternion rot, float dur)
{
    float t = 0f;
    Quaternion start = target.rotation;
    while(t < dur)
    {
        target.rotation = Quaternion.Slerp(start, rot, t / dur);
        yield return null;
        t += Time.deltaTime;
    }
    target.rotation = rot;
}
1 Like

I did mess around with lower values such as 0.3, 0.2, perhaps going even lower could fix it, but…

THIS works perfectly. Thank you so much, I cant tell you how many different pieces of code I attempted with no success, it was starting to drive me a little crazy!