Search Unity

Unity bug when converting vector 3 to quaterion?

Discussion in 'Scripting' started by Ikaro88, Jun 9, 2020.

  1. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    Hi, i try to execute this code:

    Code (CSharp):
    1.   Quaternion relative = Quaternion.Euler(RelativePosition.transform.rotation.x, RelativePosition.transform.rotation.y, RelativePosition.transform.rotation.z);
    2.         Debug.Log("before conversion" + RelativePosition.transform.rotation.x);
    3.         Debug.Log("after conversion" + relative.x);

    but i get very different numbers, for example:


    Code (CSharp):
    1.  
    2. before conversion0.6044997
    3. after conversion0.005266584
    4.  
    I am missing something or this is a bug?

    unity version 2019.1.6, nothing else touch that values
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    You're missing something. You're looking for transform.rotation.eulerAngles, or transform.eulerAngles.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
    Kurt-Dekker likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.)
     
  5. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    Thanks a lot guys, using eulerangle instead of rotation all works perfect!!

    this is closed for me!