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

[-pi, pi], [0, 2pi], strange problem

Discussion in 'Scripting' started by unity_44txmAKlHeLdxw, Dec 26, 2020.

  1. unity_44txmAKlHeLdxw

    unity_44txmAKlHeLdxw

    Joined:
    Dec 20, 2020
    Posts:
    3
    Hello everyone, so my problem is when i'm working with angle of a GameObject in unity with csharp I got a very strange problem, sometimes when my angle should be negative (-5,-6..) it's already negative so everything is fine, but sometimes this angle is working with a different functionnement, [-pi, pi], [0, 2pi] and my script isn't working like it should.

    I don't have found anything about that in the documentation, so at least I would like to fix my project with only [-pi, pi] module if you understand.

    If my english isn't correct sorry, ty.

    [ok when I Debug.Log the localEulerAngles of the object it's 350 but when I'm in the editor it shows -1°]
    It souldn't be like this i'm right?
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi,

    All transform angles are in Degrees, and the Mathf C# libraries are using Radians by default.
    Think all transform.eulerAngles, and transform.localEulerAngles returns the current angle in -180, to 180. Even if the transform has been spinning a lot of turns, or the angle has been set to another value before.
     
  3. unity_44txmAKlHeLdxw

    unity_44txmAKlHeLdxw

    Joined:
    Dec 20, 2020
    Posts:
    3
    yes sure I meant, [-180, 180] and not [-pi, pi]
    but why when I Debug.Log the localEulerAngles it shows 350
    but right now when I'm only testing Debug.Log(object.localEulerAngles.x) and moving with rotation tool it shows 190 etc... and in the editor 190
    I don't understand anything in all of this, the problem is, that I'm working a lot with rotation so that kind of things make this project more complicated.

    so debug.log object while i'm moving in editor move, same rotation but with the [0, 360] system
    and if I Debug.Log object rotation, the angle is different but the negative angle is only with the editor transform information :)

    I think I have tested all of this correctly, I can share you pictures if you want
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    It's probably best then to normalize the angles when reading them from transforms to -180 - +180, so you know what to expect, if the plan is to use the values for some kind of comparing. Another way is to use variables in the script to keep track of the angles, if the object is not moved for example by the physics engine.
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    The rotation is stored as a quaternion internally by Unity. The euler angles properties only convert from/to this quaternion and this can lead to them returning inconsistent values, since multiple euler angles values can represent the same rotation.

    You can't rely on euler angles to be in a specific range and the Unity documentation does not give any such guarantee. If you need the angles to be in a certain range, you have to normalize them yourself first.

    Generally, if possible, it's best to work with quaternions directly. This'll skip the conversion from/to euler angles and quaternion math works better with 3d rotations. You don't necessarily need to know how quaternions work, just familiarize yourself with the available operations and methods and try to combine them to get what you need.

    The Unity docs do say this regarding euler angles:
     
    PraetorBlue likes this.
  6. unity_44txmAKlHeLdxw

    unity_44txmAKlHeLdxw

    Joined:
    Dec 20, 2020
    Posts:
    3
    ok I don't have understand such why It's working so aleatoary but I will work with
    I was happy to work with something more simple, gameObject and local or global rotation it made all more simple but it's all right
    I'm gonna work with quaternion I have tested a little bit and have done few mistakes so it's all right I can work with but looking at every single vector from two gameObject and rotating isn't such fun but it's ok I will get more control of my project by working with, thank you @arfish @Adrian

    Have a nice day.