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. Dismiss Notice

Quaternion.Euler() returns wrong value

Discussion in 'Scripting' started by rj191951, Aug 27, 2019.

  1. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    Quaternion View;
    float clockwise = 90;

    void Clockwise()
    {
    Debug.Log(View.z); // shows equal to zero as expected
    View = Quaternion.Euler(new Vector(0f, 0f, View.z + clockwise));
    Debug.Log(View.z); // gives 0.707 instead of 90
    }

    Now,
    Debug.Log(View.z) shows 0.707 instead of 90
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    The "z" variable on a Quaternion does not correspond to Euler angle z; it means something completely different. To get the value you are thinking of, you would need to check "View.eulerAngles.z" instead.

    Even then, it isn't guaranteed to match, because there are multiple combinations of Euler angles that give the same net rotation, so if you convert Euler angles into a Quaternion and then back, you might not get what you started with.
     
    Joe-Censored and lordofduct like this.
  3. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    Thanks a lot, i tried

    void Clockwise()
    {
    float zrot = View.z + clockwise;
    View = new Quaternion(0f, 0f, zrot, 0f);
    }

    It worked !!!!
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Please take note of the code tags.

    What you posted might "work" in the sense that z will have the value that you set it to, but it won't produce the actual rotation that I think you expect it to produce: It does not represent a rotation of zrot degrees around the Z axis, and in fact is probably not a valid rotation at all. Did you try actually rotating an object using that Quaternion?
     
    Joe-Censored and lordofduct like this.
  5. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    No, it wasn't actually for rotating any object. The purpose was to store value(which will update at every call) and use it for a quaternion argument of a function
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    If you just need a 4-dimensional float container to store values, use Vector4.

    What is this "quaternion argument of a function"? Does that function need it for some sort of rotation calculation? Cause if not... why does it take a Quaternion?

    You plane out just should NOT modify the x,y,z,w values of a Quaternion unless you fully understand how Quaternions work (the fact you didn't recognize the significance of 0.7071, which is sqrt(2)/2, which should stand out if you have a descent grasp of trig... says to me you probably don't fully understand the maths underneath quats. Don't worry, most people around here don't).
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    If you are passing it as an argument to a function, that function is presumably going to eventually do something with it, so it still matters whether the Quaternion has the correct meaning.

    As lordofduct says, if you just need a container for floats, don't use a Quaternion. If you are being forced to use a Quaternion, then you need to make sure you use the right Quaternion, which this almost certainly is not.
     
  8. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    A function which takes an argument of type Quaternion.

    void XYZfunction(Quaternion rot)
    {
    }

    Yeah, the function needs this value for rotation calculation. And i really don't get it, why it was showing inverse of root(2)
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    It has to something to do with a normalized four-dimensional vector.... and you never need to know or care about why that is. Quaternions are complicated, and you never have to learn how they work. Seriously, you don't. It won't help you be a better game programmer. I've been using Unity for thirteen years and I'm still not sure. The Quaternion class has all the helper functions you need to manipulate rotations in Unity, and you're best off treating Quaternions as a black box and just never looking inside. All you need to know is Quaternion == rotation.

    (While we're on the subject, one thing you SHOULD know about rotations is that Euler angles are terrible and unreliable. If you want to learn something about Quaternions that will actually be useful to game programming, read this.)
     
    Joe-Censored and lordofduct like this.
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    So the Quaternion isn't just for storage... it IS for rotation. Meaning this was false:
    (though technically you may have intended to mean its for rotation, just not for rotation of any specific object)

    This code:
    Code (csharp):
    1.  
    2. void Clockwise()
    3. {
    4. float zrot = View.z + clockwise;
    5. View = new Quaternion(0f, 0f, zrot, 0f);
    6. }
    7.  
    Is not doing what you think it does.

    If you want to rotate a quaterion around the z axis, you should do something like:
    Code (csharp):
    1.  
    2. void ClockwiseGlobal()
    3. {
    4.     View = Quaternion.Euler(0f, 0f, clockwise) * View;
    5. }
    6.  
    this will rotate View by 90 degrees around the global z-axis

    if you wanted around its local z:
    Code (csharp):
    1.  
    2. void ClockwiseLocal()
    3. {
    4.     View = View * Quaternion.Euler(0f, 0f, clockwise);
    5. }
    6.  
    NOTE - I may have gotten those backwards. I'm relying on my memory for order of operations right now and I am at work working on completely unrelated topics so I might have my stuff backwards. Though I'm pretty sure I remembered that order correctly.
     
    rj191951 and Joe-Censored like this.
  11. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    This is probably the best resource to understand and better visualize quaternions: https://eater.net/quaternions
    But it is indeed not that necessary to fully understand them. It does give you a better idea of why some things won't work like just setting (0f, 0f, zrot, 0f) though.
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    As an aside: While on an unrelated tangent, I wanted to see what my very first posts on this forum were and hilariously enough... literally the first post I ever made here was me commenting that I didn't understand Quaternions back in 2006.

    Still don't. Probably never will. :)