Search Unity

Rotation isn't quite exact for some reason

Discussion in 'Scripting' started by spencer_white, Dec 28, 2016.

  1. spencer_white

    spencer_white

    Joined:
    Nov 9, 2015
    Posts:
    37
    Hi, so, I ran the following simple code:

    Code (CSharp):
    1. transform.eulerAngles = new Vector3(0f, 45f, 0f);
    Going to the Unity Inspector, I noticed that the exact rotation is 45.249. This wouldn't usually be an issue, but for this project, I would like to keep rotations nice and even, to make later additions much easier. Can anyone explain why this happens, and possibly how to fix it?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    This is probably because of floating point precision, but that's a big difference
     
  3. MarcovanderMeer

    MarcovanderMeer

    Joined:
    Oct 26, 2014
    Posts:
    2
    Did you ever figure this out? I'm getting the exact same error.

    I'm using:
    Code (CSharp):
    1. instances[i].transform.Rotate(0, 0, -45f);
    And seeing -45.249 in the inspector.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,126
    Internally Unity uses quaternions for rotation. This is a good thing as quaternions are mathematically much better for a lot of things, like blending between two rotations smoothly. Euler however is much easier for humans to deal with, so that's what's usually exposed to the user in the inspector, and most user interfaces for programs that deal with rotation regardless of their internal handling of rotations.

    However the conversion from Euler to quaternion may introduce a small amount of error due to floating point precision, and the conversion back will again, thus the number you see in the inspector or even from immediately printing out transform.rotation.eulerAngles won't necessarily match what you just put in.
     
    MarcovanderMeer and lordofduct like this.
  5. radicalEd

    radicalEd

    Joined:
    Mar 19, 2016
    Posts:
    81
    Use a quaternion instead, much better for proper rotations.

    Probably a scale issue, messing with your scales can cause all sorts of problems with pretty much any aspect of physically modifying objects.
     
    MarcovanderMeer likes this.
  6. MarcovanderMeer

    MarcovanderMeer

    Joined:
    Oct 26, 2014
    Posts:
    2
    Thanks for the help guys, I really appreciate it. I'm using Quaternions instead now and it seems to be working as expected

    Lots to learn when getting started with unity!
     
unityunity