Smooth Rotation to an specific angle

        float hMovement = Input.GetAxis("Horizontal") * movementSpeed /2;
        float vMovement = Input.GetAxis("Vertical") * movementSpeed;

        if (hMovement >  15 && 30 > transform.localRotation.eulerAngles.y ){
            transform.rotation *= Quaternion.Euler(0,2,0);
        }
        if (hMovement < -15 && -30 < transform.localRotation.eulerAngles.y){
            transform.rotation *= Quaternion.Euler(0,-2,0);
        }
        if (hMovement < 15 && hMovement > -15 ){
            transform.rotation = Quaternion.Euler(0,0,0);
        }

I am making an 3D endlessrunner in 3rd person view.
I have a problem. I want that my character rotates up to 30 degrees into the direction its moving to. And this in smooth roatations. this code kind of works. To the right side(the first if clause) it locks at 30 degrees. But at the left side it keeps spining. But why???

Never read from eulerAngles if you intend to do arithmetic on them.

Keep your own rotation and use it to drive the rotation.

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

If you need smooth rotation (or smooth ANYTHING), this approach always works:

Smoothing movement 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