So I’ve been trying to figure this out most of the day. I’ve searched a ton of forum posts and read several tutorials but none of the techniques I’ve tried works the way I’d like.
I have a 3D sidescrolling style game. I would like to have the player object rotate based on a key press from facing left (Y = 0) to facing right (Y = 180). When going from 0 to 180 it should rotate clockwise and when going from 180 to 0, counterclockwise.
That last bit is where I keep getting stuck. All solutions I’ve tried with Mathf and MoveTowardsAngle, LerpAngle, etc., always rotate clockwise. I have not been able to find any example code rotating an object both ways a specific amount and stopping.
Any help or points to articles would be greatly appreciated. Feeling pretty stuck and out of terms to Google.
Solved my own problem, I think. 
Just needed to NOT use the angle versions of those. This is what I ended up with, just in case someone else might find it useful.
if (rotating) {
float nextAngleY = Mathf.MoveTowards(transform.eulerAngles.y, targetAngleY, rotationSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, nextAngleY, transform.eulerAngles.z);
if (transform.eulerAngles.y == targetAngleY) {
rotating = false;
}
}
You’re potentially going to have problems comparing floating point. Here’s why:
Floating (float) point imprecision:
Never test floating point (float) quantities for equality / inequality. Here’s why:
https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html
https://discussions.unity.com/t/851400/4
https://discussions.unity.com/t/843503/4
“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums
I always use this pattern:
Smoothing movement between any two particular values:
https://discussions.unity.com/t/812925/5
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: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
You can see an example of it here in this project:
https://www.youtube.com/watch?v=BtT1N1lzFns
Specifically here, around line 99 where I set the desiredVisualsFacing based on facing:
https://github.com/kurtdekker/proximity_buttons/blob/master/proximity_buttons/Assets/Demo25DMovement/Level2DFollower.cs
and then line 108 where it lerps towards. I prefer the lerp feel, but you could use MoveTowardsAngle instead.