Search Unity

Issue with eulerAngles and using Mathf.Sin Cos etc. Please help

Discussion in 'Scripting' started by milint33w, Sep 4, 2012.

  1. milint33w

    milint33w

    Joined:
    Sep 4, 2012
    Posts:
    2
    Changed question and title:

    I figured out that my issue wasn't with eulerAngles giving wrong info.

    It turns out that this code
    Code (csharp):
    1. if (Input.GetKey(KeyCode.D))
    2.         {
    3.             rocket.transform.Rotate(Vector3.right * Time.deltaTime * 30);
    4.         }
    5.  
    for some reason flips the object 180 degrees on z and y the instant you rotate over 90 degrees. I need eulerAngles to report a nice continuous angle so is there another way to rotate my object where it wont for whatever reason flip the object around after 90 degrees?
     
    Last edited: Sep 4, 2012
  2. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Just convert degrees to radians (multiply to pi/180) and radians to degrees (multiply to 180/pi). The Mathf class has two constants called Deg2Rad and Rad2Deg which are exactly pi/180 and 180/pi respectively.

    Code (csharp):
    1.  
    2. GameObject myShip;
    3.  
    4. float ySin = Mathf.Sin(myShip.transform.eulerAngles.y * Mathf.Deg2Rad);
    5.  
     
    Last edited: Sep 4, 2012
  3. milint33w

    milint33w

    Joined:
    Sep 4, 2012
    Posts:
    2
    But eulerAngles doesn't go from 0 to 360 continuously. Its all broken up decreasing and increasing as you turn one direction.

    I just tried what you said anyway and it doesnt work. It works as long as the object doesnt rotate upside down but once its upside down, because the values flip flop and start decreasing, where it should be moving down it moves up and in the reverse direction.

    I can't believe how hard it is to get a continous value for rotation.
     
    Last edited: Sep 4, 2012
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Rotations are stored as quaternions...when converted to euler angles, there's more than one valid way to represent the quaternion. So reading one axis from euler angles is likely to be "incorrect". (Not really incorrect, since it's correct in context, but it doesn't really work to read one axis in isolation from the others.) The best solution is to track rotations yourself, as seen in the MouseLook script from the standard Character Controller package. This way it's guaranteed to be continuous.

    --Eric
     
  5. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    As already stated, converting degrees to radians is trivial, though it is annoying that Unity doesn't just use radians like the rest of the civilised world.

    However it sounds like your problems are really due to the nature of euler angles. It's not clear from the above quote what values you actually want to retrieve - you make it sound simple, but you are not being specific enough.

    Certainly if you're expecting to put numbers into the euler angles and later read the same numbers out, the simplest way is for you to keep your own shadow copy of the values you last set. But this doesn't work if there are other ways the object's orientation may be changed (e.g. collisions).