Make rotation smooth

i’m making a game with pretty simple controls, player moves diagonally, clicking changes the direction

if (Input.GetMouseButtonDown(0))
{
    direction *= -1;
    transform.rotation = Quaternion.Euler(0, 0, direction * 45);
}

no problems with that, was just wondering how to smooth out the rotation instead of having it just snap into place

When clicking the mouse button, instead of setting the new rotation to transform.rotation, store it in a custom variable, say targetRotation. Then in the same Update() method, but outside the Input.GetMoustButtonDown scope, you can rotate towards the target rotation like this:

transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, smootheningSpeed * Time.deltaTime);

(where smootheningSpeed is a constant float value that you set, pick a number that feels smooth for you)

And generally we call this “lerping” or “interpolating”, ie changing a value over time. There are typically two kinds of methods: Lerp (linear interpolation) and Slerp (spherical interpolation). You’ll find these in the Quaternion, Vector and Mathf classes for instance.

Smoothing the change between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

Another approach would be to use a tweening package such as LeanTween, DOTween or iTween.

OR… just make a simple canned animation… No code needed!