Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Quaternion.Euler weird rotation result

Discussion in 'Scripting' started by CarlY, Jan 11, 2016.

  1. CarlY

    CarlY

    Joined:
    Nov 5, 2013
    Posts:
    29
    I'm getting weird results from Quaternion.Euler. I don't know why this happens as honestly I don't know how quaternions really work. Here's the code.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraRotator : MonoBehaviour {
    6.  
    7.     private Camera SceneCamera;
    8.  
    9.     private void Awake()
    10.     {
    11.         SceneCamera = GetComponent<Camera>();
    12.         //RotateCamera(new Vector3(11,23,94));  //THIS WORKS
    13.         //RotateCamera(new Vector3(35, 37, 193)); // THIS WORKS
    14.         RotateCamera(new Vector3(123f, 17f, 235f)); // PROBLEM: I'm getting (57, 197, 55) as the
    15.     //euler representation of the rotation. Which is (180-123, 180+17, 235-180) but honestly
    16.     //I have no idea what's happening.
    17.     }
    18.  
    19.      public void RotateCamera(Vector3 Degrees)
    20.     {
    21.         SceneCamera.transform.rotation = Quaternion.Euler(Degrees);
    22.     }
    23. }
    24.  
    The question is very simple, what's happening here and more importantly how can I set the rotation to this Vector3(123f, 17f, 235f)
     
  2. bajeo88

    bajeo88

    Joined:
    Jul 4, 2012
    Posts:
    64
    Your angle correct, its just an oddity with Quaternions as far as i am aware.

    After a few tests ive found a few things.
    - If you use Vector3(180f, 0f, 0f) it becomes Vector3(0f, 180f, 180f)
    - Setting your rotation (123f, 17f, 235f) before you click play becomes (57, 197, 55) once playing and the gameObject does not actually change orientation

    So it appears you are setting the correct angle which is good news for you! As far as I can determine when setting a rotation (unity stores it as a quaternion internally) its inspector value is converted back from quaternion and so appears different in some cases. Quaternion is a point on a sphere based on 4 inputs, x,y,z and a time style value to blend between them, as to why it is a different value when reversing the calculation it would require someone with a bigger curiosity than me i'm afraid. Been a while since i read about them so my description may be off a little...

    A nice video I watched a while back gave me some good knowledge on quaternions (
    )
     
    senkal_ likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    Try a little experiment. Create two GameObjects (empty, cubes, whatever). In the inspector, set one of them to (123,17,235), and the other one to (57,197,55). Notice anything about those rotations? They're identical.

    Here's the trick: Going from Euler -> Quaternion is deterministic. There's only one Quaternion that any given Euler angles can convert to. However, going from Quaternion -> Euler is not. Any given rotation has a huge number of ways to be represented by Euler angles. You've just discovered a pair of them. You can obviously add or subtract any any multiple of 360 to any of the numbers without affecting the result. When converting Quaternion -> Euler, any of these sets of Euler angles is a perfectly valid result. Unity's algorithm just sort of picks one.

    Internally, Unity stores all rotations as Quaternions. When you do this:
    Code (csharp):
    1.         SceneCamera.transform.rotation = Quaternion.Euler(Degrees);
    Immediately after this line, those Euler angles are forgotten, forever. Anytime you get Euler angles back in the future (for example, when you look at it in the Inspector), it's converting from the Quaternion back to Euler angles.

    The only exception to this is the Inspector itself. When you type in (123, 17, 235) in the inspector, the inspector itself remembers those three numbers for the sake of user friendliness: as long as the Quaternion isn't changed elsewhere, it'll display those three numbers instead of doing a Quaternion -> Euler conversion.

    tl;dr: Just because the Inspector is displaying unexpected numbers doesn't mean that the rotation is wrong.
     
  4. CarlY

    CarlY

    Joined:
    Nov 5, 2013
    Posts:
    29
    Fantastic answers, both of you. Thanks a lot!
     
  5. iewufhe

    iewufhe

    Joined:
    Aug 2, 2013
    Posts:
    11