I'm making a rotation script for a slot machine and it works well with few bugs: a Floating point error that causes it to avoid landing squarely on a number (landing on 59.99997 instead of 60.0).
I need a way to remedy this and I've come up with two solutions:
1.) Compare two numbers to see if they equal each other, plus or minus 0.50. The current script compares the transform.rotation.
2.) Use Quaternion Slerp on a specific EulerAngle(EulerAngle.x alone to be exact)
I heard that rotation done by Quaternion Slerp could stop floating point from appearing.
I know how to compare two numbers but not with an additional range of 0.50.
Can someone show me how to do either one or present some other solution?
The following code works for me.
Simply convert your float to an int.
int xAngleInt = 0;
float xAngle = 0;
void Update()
{
xAngle = 59.9994F; // Set your random angle here
xAngleInt = Convert.ToInt32(xAngle);
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 100), xAngleInt.ToString());
}
In this case the value of '60' will always be shown. So no matter what xAngle is set to in your Update() method, your true integer value will only be shown.
Don't use transform.eulerAngle, it won't work if the rotation is more then 360 degrees and if you're making a slot machine I imagine you'll want it to spin more then that. Use transform.Rotate instead. But in this case I would just use
Mathf.RoundToInt(59.99997) //returns 60
It will return the nearest int. If it's exactly in between two ints (like 1.5 for instance) it will return the even one of the two, so.. two in this case ^.^
You could keep the angle from last frame, and compare the two. If that difference is small (you pick how small) you decide that it's done moving, then set it equal to the nearest integer and set your display accordingly.