Search Unity

Euler angles and Quaternions: my head is spinning

Discussion in 'Scripting' started by JD_Designer, Aug 22, 2017.

  1. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Hey everyone,

    I can't wrap my head around rotations. I simply want to say: if targetObject's rotation is less than 90 and greater than -90 (as displayed in the editor) than execute something. But I can't get it to work and I keep getting strange results. Its my understanding (please correct me if I'm wrong and explain to me like I'm 5) that Unity stores rotations as Quaternions internally and what you see in the editor in the transform component are euler angles. But I must be missing something. My code below yields bizarre results.

    Code (CSharp):
    1. Transform targetObject;
    2.  
    3. if (targetObject.eulerAngles.x < 90 && targetObject.eulerAngles.x > -90)
    4. {
    5. //do something
    6. }
    Thanks in advance!
     
  2. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    I'm also seeing now that if I do this...
    Code (CSharp):
    1. public Transform targetObject;
    2. public Vector3 targetObjectRot;
    3.  
    4. targetObjectRot = targetObject.localRotation.eulerAngles;
    ... and hit run, targetObjectRot displays the correct values until I rotate targetObject into the negative (lets say -9 on the x axis) at which point targetObjectRot displays the number 351 for the x axis. So how do I get around this?

    Edit: found out how to calculate negative euler angles here. Rotating things in Unity!
     
    Last edited: Aug 22, 2017
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Because the euler angles are constrainted from 0 to 360 it means you won't get any negative angles. If you want -90 you would need to use 270 instead (360-90)

    So your check would be:

    if( ( angle.x > 270 ) || ( angle.x < 90 ) )
    {
    // Angle between -90 and +90
    }
     
    JD_Designer likes this.
  4. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Thanks friend! I see that now. For anyone else who stumbles on this thread, I should also add that the rotation values you see in the editor are in fact the local euler angle values.
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Yes, Unity stores rotations as Quaternions and displays the Euler angles in the editor.

    First, there's a difference between transform.rotation and transform.localRotation. Local Rotation is the Quaternion for what you see in the Unity editor, and it's the rotation relative to its parent. Rotation is the total rotation, that you would get from adding its local rotation and all of its parents' rotations. Similarly, localEulerAngles is the local rotation in Euler form (what you actually see in the unity editor) and eulerAngles is the combined rotation including all the parents.

    As for your second post, -9 degrees is the same thing as 351 degrees. You could check if it's either between -90 and 90 or between 270 and 360. Simpler might be to use Mathf.DeltaAngle ( https://docs.unity3d.com/ScriptReference/Mathf.DeltaAngle.html )

    Code (csharp):
    1. if (Mathf.Abs(Mathf.DeltaAngle(transform.localEuler.x, 0)) < 90f)
    2.    //do stuff
    Delta angle will get the smallest distance between the angles regardless of how they're expressed (-9 or 351 will be treated the same). Here I'm getting the absolute value of the difference between the angle and 0, and checking if it's less than 90, which should work for you.
     
    JD_Designer likes this.
  6. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Thank you so much!
     
    makeshiftwings likes this.