# rotation floating point error

Hello Folks,

I am trying to rotate a 2D-gameObject about 90° by clicking a button. So the rotation should stop at 90, 180, 270, 360.
This works perfectly fine, but when it turns around it does not stop at the degrees 90, instead it stops by 90,000002F or when it turns to zero it totally turns crazy, and the number stops at -1.001791e-05.
I allready know its the floating point error and i know why the computer writes the number not correct. but i want to make my code perfect because otherwise my controlls will not work.Heres my code… the degree variable will get -90F or +90F from another script when the specific button is clicked. Thanks a lot…

``````using System.Collections;

public class RotateScript : MonoBehaviour {

float damping = 9F;
public static float degree = 0;
Vector3 rotation;

void Start(){

}

void Update () {

rotation = transform.eulerAngles;

this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.Euler (0, 0, degree), Time.deltaTime* damping);

if (Mathf.Approximately (rotation.z, 0F)) {

rotation.z = 0;

}
if (Mathf.Approximately (rotation.z, 90F)) {

rotation.z = 90;

}
if (Mathf.Approximately (rotation.z, 180F)) {

rotation.z = 170;

}
if (Mathf.Approximately (rotation.z, 270F)) {

rotation.z =270;

}
if (Mathf.Approximately (rotation.z, 360F)) {

rotation.z = 0;

}

}

}
``````

You have to manually check and clamp the rotation if you are using Quaternion.Slerp.
And your degree variable keeps incrementing by +90 when degree = 360 you have to reset it to 0.