# Rotate character 180 degrees determine direction

In my game it should be possible to rotate the character 180 degrees. I always want the character to rotate clockwise.

If the character is rotated 0 degrees and I press the rotate button, the character rotates clockwise to 180 degrees. However, if I know press the rotate button again, the character rotates to 0 degrees but doing so counterclockwise. How can I modify my code so that the character always rotates in a consistent direction?

One solution I've been thinking of is to do the rotaiton in steps like on first button press { 90, 180 } and on second {270, 0 }...

Any help would be greatly appreciated!

_currentTurnAroundAngle += TurnAroundSpeed*Time.deltaTime;

transform.rotation = Quaternion.Euler(0, Mathf.Lerp(transform.rotation.eulerAngles.y, _targetTurnAround, _currentTurnAroundAngle), 0);

if (transform.rotation.eulerAngles.y >= (_targetTurnAround - 0.1f) && transform.rotation.eulerAngles.y <= (_targetTurnAround + 0.1f))
{
_currentTurnAroundAngle = 0;
transform.rotation = Quaternion.Euler(0, _targetTurnAround, 0);
_targetTurnAround = (_targetTurnAround == 0 ? 180 : 0);
_isTurning = false;
}


Try Mathf.LerpAngle(). So, this line becomes:

transform.rotation = Quaternion.Euler(0, Mathf.LerpAngle(transform.rotation.eulerAngles.y, _targetTurnAround, _currentTurnAroundAngle), 0);

Some suggestions:

1. In general, you should keep the start and end angles, and lerp between them. Right now you are lerping from the current angle rather than the start angle.

2. When you have two angles and you want to make an operation between them (add, subtract, lerp, compare, etc.), you should move them to the same 360 region. For example, if your angles are -300 and 300, you should convert the first -300 to 60, which represents the same angle.

Here is a way I do that:

float angle1 = -300;
float angle2 = 300;
while (angle1 - angle2 > 180) angle2 += 360;
while (angle2 - angle1 > 180) angle1 += 360;
Assert.IsTrue(Mathf.Abs(angle1 - angle2 <= 180));

• So easy! Thanks, works great! I also noticed now, if I had set the target to 360 instead of 0 it would work as expected. Jul 27 '16 at 14:25

Could you not simply use

transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);


And have it stop rotating once it hits 180 degrees? As I recall, transform.Rotate will always rotate clockwise unless you provide it with a negative number, in which case it will rotate counterclockwise.

Or, a different approach would be to go between from 0 to 180 and from 180 to 360, rather than from 0 to 180 and from 180 to 0. Since 0 = 360, you could just reset your rotation after the second turn.