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

Weird rotation degrees

Discussion in 'Scripting' started by TheDeathKnight, Aug 10, 2015.

  1. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Hey guys,

    I'm trying to rotate my character and I just note something weird about the rotation.

    When I rotate my character forward, the first degrees are 0 to 90 then 90 to 0 and finally 270 to 360 then 360 to 270.

    The character rotation looks good, but why those numbers, why not 0 to 360 ?

    Code (CSharp):
    1.     if (Input.GetMouseButton (0)) {
    2.  
    3.  
    4.  
    5.            
    6.             transform.RotateAround(Target.position, new Vector3 (Input.GetAxis ("Mouse Y"), Input.GetAxis ("Mouse X"), 0f), 300 * Time.deltaTime);
    7.        
    8.  
    9.         }
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Unity stores rotation as Quaternions, which are similar to Vector4s. With simple x,y,z rotations (also called Euler angles), you can end up with a problem called Gimbal Locking, which is why we use these seemingly overcomplicated Quaternions.

    I won't bore or confuse you with how quaternions work, but basically the quaternion for the rotation (90, 0, 0) is exactly the same as the quaternion for the rotation (-270, 0, 0) and (450, 0, 0). When Unity sees that quaternion, there is an infinite number of ways it display it to you, so it takes it's best guess at what you want to see.



    Trust me when I say this inconvenience is preferable to Gimbal locking.
     
  3. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Alright, I understand now.

    However, I don't know where and when I should use quaternion. Can I use quaternion instead euler angles anytime ?

    Secondly, I'm wondering why with RotateAround (the code above) my camera rotate on the Z axis with Mouse X when I rotate my character on the left or on the right. That's pretty tricky to explain, but basically if I don't rotate my character the X rotation works well.

    Thanks.
     
  4. Lorinthio

    Lorinthio

    Joined:
    Aug 7, 2015
    Posts:
    39
    Quaternion is the hardset gameobject for Unity in terms of rotation. Using Quaternion.eulerAngles(), returns the quaternion you need for setting rotation based on "normal" angles that we are used to using.

    In terms of your second question, I've not checked this myself, but wouldn't you want Transform.Rotate()? Not sure on the exact setup of your game of course. But I usually use Rotate()
     
  5. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    I would like to use Rotate, but I don't know how to make a mouseorbit by myselft using Transform.rotate.


    Code (CSharp):
    1. void Update () {
    2.  
    3.         OriginRot = Target.rotation;
    4.         OriginRot *= Quaternion.Euler (25f, 0f, 0f);
    5.  
    6.         Origin = Target.TransformPoint (new Vector3 (0f, 8f, -15f));
    7.        
    8.         if (Input.GetMouseButton (0)) {
    9.  
    10.  
    11.         transform.RotateAround(Target.position, new Vector3 (Input.GetAxis ("Mouse Y"), Input.GetAxis ("Mouse X"), 0f), 300 * Time.deltaTime);
    12.  
    13.  
    14.  
    15.         }
    16.         else
    17.         {
    18.             transform.position =  Vector3.Lerp (transform.position, Origin, Time.deltaTime * 5);
    19.            
    20.             transform.rotation = Quaternion.Slerp (transform.rotation, OriginRot, Time.deltaTime * 5);
    21.  
    22.  
    23.            
    24.         }
    25.        
    26.     }
    27. }
     
  6. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398