Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Rotation gives negative value when button is pressed

Discussion in 'Scripting' started by Alex_TNT, Jun 18, 2015.

  1. Alex_TNT

    Alex_TNT

    Joined:
    Dec 26, 2011
    Posts:
    231
    When I use this code and I press 180positive value it makes the object -180 rotate on y.
    I don't understand why Vector3.up*90 till Vector3.up*170 gives positive value, but once is Vector3.up*180 gives negative value. Any thoughts?
    Code (csharp):
    1.  
    2.     if(GUILayout.Button("Test Button"))
    3.   {
    4.      Debug.Log (Vector3.up*180);
    5.   }
    6.  
    outputs (0.0, 180.0, 0.0)


    Code (csharp):
    1.  
    2.   GUILayout.BeginHorizontal();
    3.    var obj = Selection.activeGameObject;
    4.   if(GUILayout.Button("180ยบ")){
    5.      obj.transform.Rotate(Vector3.up*180);
    6.   }else if(GUILayout.Button("90ยบ")){
    7.      obj.transform.Rotate(Vector3.up*90);
    8.   }else if(GUILayout.Button("-90ยบ")){
    9.      obj.transform.Rotate(Vector3.down*90);
    10.   }else if(GUILayout.Button("-180ยบ")){
    11.      obj.transform.RotateAround(obj.transform.position,obj.transform.up,180);
    12.   }
    13.    GUILayout.EndHorizontal();
    14.  
    15.  
     
  2. Alex_TNT

    Alex_TNT

    Joined:
    Dec 26, 2011
    Posts:
    231
    As a quick fix seems like if I do double 90 on one press of a button it gets a positive value

    Code (csharp):
    1.  
    2.     if(GUILayout.Button("180ยบ")){
    3.      obj.transform.Rotate(Vector3.up*90);
    4.      obj.transform.Rotate(Vector3.up*90);
    5.   }
    6.  
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm a little confused, there is no "movement over time" in that code, so 180 or -180 is still going to be the exact same instant rotation... why are you concerned about "negative values", and where are you seeing them given there is no debug information in the code?
     
  4. Alex_TNT

    Alex_TNT

    Joined:
    Dec 26, 2011
    Posts:
    231
    This is not a code that runs at run time it's a window GUI element. It rotates selected object at fixed amount on button press. Output is in Inspector window transform -> rotation -> y axis

    I select an object, press the button, Vector3.up = 180 it's rotates the object in negative value that's -180 y axis(horizontal axis) the negative value should be from Vector3.down = 180

    tl;dr
    this is in EditorWindow
    Vector3.up = 180 should be 180 instead I get -180
    Vector3.down = 180 is -180
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    180 = -180 in true angle, so they're interchangeable. When two notations of the same number equal the same real rotation, the program picks one arbitrarily. In your case, that's -180. It doesn't affect the rotation in any way, as it's still where you'd rotate it to.

    A good way to think about it is turning around. If you turn around to the left, -180, you're facing the same way as if you turn around to the right, 180.