- Home /
Weird rotation precision
Hi, I have script where I try to rotate my object to desired position, but the object doesnt have the exact angle I want, and its almost 1 degree of difference. I even wrote in script:
Quaternion _newRotation = connector.transform.parent.parent.localRotation;
_newRotation.y = 180;
connector.transform.parent.parent.localRotation = _newRotation;
But the rotation of object still isn't exact 180
I have never faced something like that. I know about floating point precision but most of the time its difference of about 0.00001 , not almost 0.7 How is that happening and how can I prevent this? Because of this I can't properly merge rooms for procedural dungeon generation
Answer by Bunny83 · Aug 10, 2017 at 02:12 PM
A Quaternion is a 4 dimensional complex number and do not represent euler angles.
You do not need to fully understand how quaternions work, but if you don't you shouldn't mess with it's 4 components. You can use Quaternions only by using the various static methods it provides.
You haven't presented how you actually create your rotation and how you define your "desired position".
You can create an absolute or relative rotation based on eulerangles by using Quaternion.Euler
. Though in many cases you usually use things like Quaternion.LookRotation
, Quaternion.AngleAxis
or Quaternion.FromToRotation
to create a rotation. For more details see the documentation
So, rather than:
Quaternion _newRotation = connector.transform.parent.parent.localRotation;
_newRotation.y = 180; //this is the bad line- I suspect, you dont want to change the Y value of the quaternion you want to change the "rotatation about the Y axis" (one of the three euler angles).
connector.transform.parent.parent.localRotation = _newRotation;
try this (uncompiled/untestest):
Vector3 eulerAngles= connector.transform.parent.parent.localRotation.eulerAngles;//NOTE: the Vector3 usered here is not a "regular" Vector3 (direction, length), its simply a convient place to store three angles (in degrees)
eulerAngle.y=180;// notice we are changing the Y value of a Vector3, not a quaternion.
connector.transform.parent.parent.localRotation= Quaterion.Euler(eulerAngles);// creates a new Quaterion using these euler angles and assigns it to the localRotatation.
Yes guys, I made a huge mistake in which I used quaternions ins$$anonymous$$d of Vector3 representing euler angles. Thank you Bunny83 and Glurth!
Answer by TGamingStudio · Aug 10, 2017 at 02:09 PM
probably it is Child with something . than it have some weird rotation .