Rotation of object unity, rotating with the wrong value

My problem is that I want to be able to rotate the cylinder 9 times. 360/9 - 40, so I just need to rotate 40 degrees 9 times. However, this does not work, because when I rotate the cylinder for the first time, and not 40 degrees, it rotates 39.99 degrees. This happens at other turns.

Im spinning while doing this.

if(Input.GetKeyUp("i")) transform.GetChild(m_Section).Rotate(0,40.0f,0); 

I have version 3.4, this is not pro and Im coding in C #.

Any help is appreciated as I just started trying to learn unity.

+6
source share
2 answers

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.

+1
source

Take a look at Kungfooman's answer in this post , he describes a problem with turning over 90 degrees or 90 degrees, as well as 270 degrees. It provides an extension that will always calculate the correct Quaternion pendant for the value you want to set. Look at here:

 using UnityEngine; using System.Collections; public class ExtensionVector3 : MonoBehaviour { public static float CalculateEulerSafeX(float x){ if( x > -90 && x <= 90 ){ return x; } if( x > 0 ){ x -= 180; } else { x += 180; } return x; } public static Vector3 EulerSafeX(Vector3 eulerAngles){ eulerAngles.x = CalculateEulerSafeX(eulerAngles.x); return eulerAngles; } } 

And I used it like this:

 Quaternion newQuat = m_directionalLight.transform.rotation; Vector3 nL = new Vector3(ExtensionVector3.CalculateEulerSafeX(xNewValueLight), 0, 0); newQuat.eulerAngles = nL; m_directionalLight.transform.rotation = Quaternion.Lerp(m_directionalLight.transform.rotation, newQuat, Time.deltaTime); 
0
source

Source: https://habr.com/ru/post/903524/


All Articles