Unity3D stores rotations in a rather abstract mathematical representation called quaternion . Tranformation from Euler angles to Euler (which you see in the Unity editor) includes a cascade of trigonometric functions and, therefore, is subject to rounding errors, especially for a simple float type.
To work around this problem in your case, I recommend saving the original Quaternion object before starting the rotation and setting it at the end of the process. Some pseudo codes:
public class RotationController : MonoBehaviour { Quaternion rotationAtStart; int numOfRotations = 0; void rotate () { numOfRotations++; if (numOfRotations == 1) { rotationAtStart = transform.rotation; } else if (numOfRotations < 9) { transform.Rotate (new Vector3 (0,40.0f,0)); } else if (numOfRotations == 9) { transform.rotation = rotationAtStart; } } void Update () { if (numOfRotations < 9) { rotate (); } } }
The special 360 ° situation makes this approach stable. For less than 360 ° you will have to live with small rounding errors. In this case, I would recommend calculating the Quaternion.RotateTowards target quaternion and installing it at the last stage, analog to the 360 case.
Another useful thing for you is Animation . You can define the animation as smooth or discrete, and simply call GameObject.animation.Play("MyRotation") if you press "i". Use the AnimationEvent at the end to get information about the completion of the animation.
Finally, Mathf contains an Approximately function that addresses the issue of floating point inaccuracies.
source share