# How am I supposed to use Euler angles?

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

Joined:
Dec 16, 2017
Posts:
29
2. ### dgoyette

Joined:
Jul 1, 2016
Posts:
1,697
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.

SparrowsNest and Kurt-Dekker like this.
3. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
3,681
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.

SparrowsNest likes this.
4. ### Doug_B

Joined:
Jun 4, 2017
Posts:
1,584
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.

5. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
3,681
Otherwise you'll just be spinning...

(I'll let myself out.)

Doug_B likes this.
6. ### AndersMalmgren

Joined:
Aug 31, 2014
Posts:
4,297
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.     }

7. ### dibdab

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

this is very disappointing.

5.65 project Quaternion.AngleAxis

#### Attached Files:

• ###### quaterngimbaltest.unitypackage
File size:
6.2 KB
Views:
5
Last edited: May 15, 2019
8. ### Owen-Reynolds

Joined:
Feb 15, 2012
Posts:
578
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.

Joined:
Apr 13, 2014
Posts:
592
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.