Hi, i try to execute this code:
Quaternion relative = Quaternion.Euler(RelativePosition.transform.rotation.x, RelativePosition.transform.rotation.y, RelativePosition.transform.rotation.z);
Debug.Log("before conversion" + RelativePosition.transform.rotation.x);
Debug.Log("after conversion" + relative.x);
but i get very different numbers, for example:
before conversion0.6044997
after conversion0.005266584
I am missing something or this is a bug?
unity version 2019.1.6, nothing else touch that values
You’re missing something. You’re looking for transform.rotation.eulerAngles, or transform.eulerAngles.
You are confusing the x/y/z of euler angles with the x/y/z/w of Quaternions. They are not equivalent. In fact, you should basically never get or set x/y/z/w of a Quaternion directly, unless you have a lot of advanced knowledge of how they work.
1 Like
Also, it is important to note that Euler angles themselves may not reliably give you the same number if you convert Euler → rotation → Euler - not due to any kind of bug or anything, just the mathematical nature of how Euler angles work. Longer explanation here. tldr is that Euler angles are terrible for 90% of use cases, generally the only time you should ever use them is when a human needs to type in a rotation value.
(This doesn’t directly apply to your sample code, because [assuming you fix what the others have said] you’re going rotation → Euler → rotation, which DOES reliably produce the same result, at least within the margin of floating point inaccuracy. However, it’s important to know this fact of Euler angles and I’m guessing that this is not the only rotation-related logic you’re doing.)
Thanks a lot guys, using eulerangle instead of rotation all works perfect!!
this is closed for me!