Search Unity

How am I supposed to use Euler angles?

Discussion in 'Scripting' started by Hotpockets26, Jun 12, 2018.

  1. Hotpockets26

    Hotpockets26

    Joined:
    Dec 16, 2017
    Posts:
    32
    mgnetic likes this.
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I don't consider it inappropriate to set eulerAngles. You just have to be aware of the limitations. Euler angles are pretty convenient when you're adjusting a single axis and you want to set it to some specific angle, since euler angles are a lot more intuitive than quaternions. The downside to euler angles is that you get unexpected behavior depending on the rotation, due to gimbal lock. Basically, apply a couple of 90' rotations on two axes and the third axis becomes "locked", and change it doesn't affect the rotation.

    If you're using rotation to point around to arbitrary angles in a 3D environment, you're better off using Quaternions, which don't suffer from gimbal lock. But if you're just rotating something on a single axis, or something simple, euler angles are likely fine. If you want to explain what you're using eluer angles for, someone can probably advise you on whether quaternions would be a better choice for you.
     
    Bunny83, SparrowGS and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Euler angles cannot specify a rotation fully, whereas a Quaternion does. Euler angles are subject to "gimbal lock" when one of the angles reaches or exceeds 90 degrees.

    For instance, if you rotate around the X axis by 90 degrees, now what was formerly the Y rotation is identical to the Z rotation if you are using Euler angles. That's gimbal lock.
     
    Darkcoding and SparrowGS like this.
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Both previous answers are spot on. One other thing maybe worth adding is that whilst Quaternions do not suffer from gimbal lock (like Euler angles do), Quaternions cannot handle relative angles greater than 180 degrees like Euler angles can.

    If you want a fuller explanation, then you could have a quick read of Rotation and Orientation in Unity.

    The long and the short of it is (as the others have already said), don't be scared to use either- but just make sure that whichever way you use, try to use it for the right reason in the right way. :)
     
    Bunny83 likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Otherwise you'll just be spinning...

    (I'll let myself out.)
     
    omartemsamani22 and Doug_B like this.
  6. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Euluers are usually used for local space rotation on a single axis, then its fine and gimbal lock is not a issue. Example form our game

    Code (CSharp):
    1. public abstract class Trigger : MonoBehaviour
    2.     {
    3.         protected abstract void UpdateTransform(float ratio);
    4.         private float lastRatio = -1;
    5.  
    6.         public void SetTriggerRatio(float ratio)
    7.         {
    8.             if (Math.Abs(ratio-lastRatio) > float.Epsilon)
    9.             {
    10.                 lastRatio = ratio;
    11.                 UpdateTransform(ratio);
    12.             }
    13.         }
    14.     }
    15.  
    16.     public class RotatingTrigger : Trigger
    17.     {
    18.         public float FireAngle;
    19.  
    20.  
    21.         protected override void UpdateTransform(float ratio)
    22.         {
    23.             transform.localEulerAngles = new Vector3(ratio * FireAngle, 0, 0);
    24.         }
    25.     }
    26.  
    27.     public class TranslatingTrigger : Trigger
    28.     {
    29.         public Transform EndPoint;
    30.         private Vector3 startPoint;
    31.  
    32.         protected virtual void Awake()
    33.         {
    34.             startPoint = this.transform.localPosition;
    35.         }
    36.  
    37.         protected override void UpdateTransform(float ratio)
    38.         {
    39.             transform.localPosition = Vector3.Lerp(startPoint, EndPoint.localPosition, ratio);
    40.  
    41.         }
    42.     }
     
    clamum, Bunny83 and That1phillps like this.
  7. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    you sure there's no gimbal lock in quaternions?

    this is very disappointing.

    5.65 project Quaternion.AngleAxis
    angleaxis.gif
     

    Attached Files:

    Last edited: May 15, 2019
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    The trouble is in trying to read them back after setting them. You'll often get different values. For example, this won't stop, x will never get past 90, but it will perfectly tilt backwards on x:
    Code (CSharp):
    1. Vector3 ang=transform.rotation.eulerAngles;
    2. if(ang.x<180) {
    3.   ang.x+=1;
    4.   // print(ang.x);
    5.   transform.rotation=Quaternion.Euler(ang);
    6. }
    It goes up to 90, then _back down_ to 0. Reading the angles after setting them gets you rejiggered values.

    If you save ang as your own global, never reading it from your rotation, it will work fine.
     
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Using Quaternions doesn't preclude you from gimbal lock. If you are dealing with Quaternions in terms of Euler angles, you will still experience gimbal lock.

    However that doesn't look like gimbal lock is the (only) issue.
     
    Bunny83 likes this.
  10. That1phillps

    That1phillps

    Joined:
    May 16, 2018
    Posts:
    10
    Saved my life, brotha! Thanks!
     
    MDADigital likes this.
  11. clamum

    clamum

    Joined:
    May 14, 2017
    Posts:
    61
    Thanks big dawg that helped me
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Here's more on this long beaten-to-death subject:

    Notes on clamping camera rotation and NOT using .eulerAngles because of gimbal lock:

    https://forum.unity.com/threads/implement-a-limit-in-camera-movement.1100038/#post-7082380
    https://forum.unity.com/threads/implement-a-limit-in-camera-movement.1100038/#post-7083097

    How to instantly see gimbal lock for yourself:

    https://forum.unity.com/threads/confusing-bug.1165789/#post-7471441

    All about Euler angles and rotations, by StarManta:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
     
    SatansPineapple likes this.