How to create smooth rotation animation instead of instant rotation?

I am new to Unity and I am currently making a rail shooter spaceship game (like Star fox), and I struggle to make the tilting movement smooth. My intention is to tilt my spaceship towards the left (z-axis) when its moving to the right, vice versa. Thinking this would make the movement more realistic and appealing.


The tilting in the z-axis starts gradually rising from 0 to 80 as long as I hold down the input, and gradually drop back down to 0 upon release. However, if I instantly press the input for the opposite direction, the tilting movement instantly return to 0 instead of gradually, then proceed gradually decrease to -80. Which make the movement looks glitchy.


I want the tilting animation to only start playing when the z-axis rotation have returned to 0 instead of playing it instantly upon pressing the input.


if(transform.rotation.z <= 1 || transform.rotation.z >= -1)
        {
            float HorizontalRotationInput = CrossPlatformInputManager.GetAxis("Horizontal") * HorizontalRotationSpeed;

            float HorizontalRotationRaw = transform.localRotation.z + HorizontalRotationInput;

            transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localPosition.y, HorizontalRotationRaw);
        }

transform.localRotation is not meant to be used in the way you are trying to use it.
_

_
“Unity stores rotations as Quaternions internally.” Your code will not work in a standalone build.
_
You should be using localEulerAngles
_

_
As for the speed/smoothness issue you need to rotate based on deltaTime.
_

_
“The completion time in seconds since the last frame (Read Only).”
_
currentEulerAngles += new Vector3(x, y, z) * Time.deltaTime * rotationSpeed;