# iTween - Rotation Add get's uneven after several rotations

I hope it’s alright if I post an iTween question here… Either way, here it is.

My current setup is a camera on my screen, that is set to rotate 90 degrees when you hit the right or left arrows. (it rotates 90 degrees left, with the left arrow, 90 degrees right with the right arrow.)

Here lies my problem. If I rotate it to the left say, it will be fine. Same for the right. But if I keep rotating it different directions, eventually, instead of being a solid number (remember, all I’m doing is adding 90 degrees left and right) it’s something like 180.4532 degrees, or 90.346. If I keep doing this, it get’s worse. All I’m doing is using RotationAdd to add 90 to the Z of my camera when I press a key. Why would it be doing this?

-Thanks

Anytime you have a relative rotations, there will be drift. Part of the drift is related to the the imprecision of floating point numbers. In addition, it is easy when coding relative rotations to under or overshoot a bit on the last frame. Regardless of why it is happening, the solution is to use an absolute rotation. If you are only rotating by 90 on one axis, then keep a integer count of the number of 90 degree turns. Add or subtract one from the integer count and then use the result in an absolute rotation. For example iTween.RotateTo(). Or you could keep your own float value and round it to 90 each time you increment/decrement it.

Thanks for the snippet above! I wanted to rotate the object regardless if it was currently rotating or not so I just did this:

`````` public class Example : MonoBehaviour {

private int rotCount = 0;
private bool rotating = false;

void Update() {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
rotCount = (rotCount + 1) % 4;
DoRotation();
}

if (Input.GetKeyDown(KeyCode.RightArrow)) {
rotCount = (rotCount - 1) % 4;
DoRotation();
}
}

void DoRotation() {
float rot = rotCount * 90.0f;
if(rotating){
//If the object is currently rotating, instantly rotate it to its destination
iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",0));
}
rotating = true;
iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",1.25));
}

void EndRotation() {
rotating = false;
}
}
``````