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

Fixing negative rotations

Discussion in 'Scripting' started by Wolfos, Jan 10, 2012.

  1. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    934
    Trying to make it so that when I press the forward key, the player turns in the direction of the camera.
    While it works when the camera has a positive rotation, it doesn't when it has a negative rotation. How do I fix it?
    Tried this:
    Code (csharp):
    1.  
    2. if(Input.GetAxis("Vertical")>0){
    3.             transform.rotation=Quaternion.Euler(transform.rotation.x,cam.rotation.y*180,transform.rotation.z);
    4.             if(cam.rotation.y <0){
    5.                 Quaternion temprot = Quaternion.Inverse(cam.rotation);
    6.                 transform.rotation=Quaternion.Euler(transform.rotation.x,temprot.y*180,transform.rotation.z);
    7.             }
    8.         }
    9.     }
    10.  
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Welcome to Gimbal Lock. Use transform.Rotate() :)
     
    Joe-Censored likes this.
  3. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    934
    Tried a different method, but there's now a similar issue.
    Code (csharp):
    1.  
    2. float rotdif = cam.rotation.y * 360 - transform.rotation.y*360;
    3.         //print(rotdif);
    4.         if(rotdif > 20){
    5.             transform.Rotate(0,rotspeed*Time.deltaTime,0);
    6.         }
    7.         if(rotdif < -20){
    8.             transform.Rotate(0,-rotspeed*Time.deltaTime,0);
    9.         }
    10.     }
    11.  
    When the camera rotation becomes close to 360/0 again, we'll start rotating the wrong way because obviously, 0 is less than 20.
     
  4. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
  5. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    934
    Nope.
     
  6. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    Try multiplying the negative rotation by -1.
     
  7. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    934
    Considering - * - = +, that would not work. -270 would become 270 while the rotation actually equals 90.
    EDIT: obviously, adding 360 would properly do that XD.

    Now I need to know how to get it to work when near 0.
     
    Last edited: Jan 12, 2012
    galatanubogdan14 likes this.
  8. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    934
    Let's add a bump. I still have no idea how I work the switch between 359 and 0.
     
  9. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    There are a few tools, which may be useful for you

    Mahtf.Repeat:
    http://unity3d.com/support/documentation/ScriptReference/Mathf.Repeat.html

    With repeat you can make sure that a certain value is always between 0-n. So if you have
    Code (csharp):
    1.  
    2. float angleX = Mathf.Round(transform.rotation.eulerAngles.x, 360.0f);
    3.  
    angleX will always be between 0 and 359. Every time it gets above that it will start at 0 and if its negative it will "repeat" itself until its between 0 and 360.

    Second your code above seems wrong. You're using Quaternion.Euler(...) but passing quaternion values to into int instead of euler angles.

    transform.rotation is a quaternion, and has 4 axis (x, y, z and w which are between 0 and 1 (iirc, maybe also -1 to 1). If you want to pass euler angles, you must use: transform.rotation.eulerAngles.x instead of transform.rotation.x
     
  10. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    Wouldn't it work if you multiply the rotation with -360 (and if needed divide by 360 afterwards) if the number is negative?
     
    Last edited: Jan 14, 2012
  11. Riderfan

    Riderfan

    Joined:
    Jan 10, 2013
    Posts:
    514
    Code (CSharp):
    1. /// <summary>
    2.     /// Clamps any angle to a value between 0 and 360 : -90 = 270
    3.     /// </summary>
    4.     public static float ClampAngle(float _Angle)
    5.     {
    6.         float ReturnAngle = _Angle;
    7.  
    8.         if (_Angle < 0f)
    9.             ReturnAngle = (_Angle + (360f * ((_Angle / 360f) + 1)));
    10.  
    11.         else if (_Angle > 360f)
    12.             ReturnAngle = (_Angle - (360f * (_Angle / 360f)));
    13.  
    14.         else if (ReturnAngle == 360) //Never use 360, only go from 0 to 359
    15.             ReturnAngle = 0;
    16.  
    17.         return ReturnAngle;
    18.     }
     
    Curipoc and Wolfos like this.
  12. galatanubogdan14

    galatanubogdan14

    Joined:
    Apr 6, 2018
    Posts:
    1
    In my case, I just decreased 360 from my angle.
    Code (CSharp):
    1. if (rotationX > 300f)
    2.     rotationX -= 360f;
    3. //Where rotationX is the angle measured in float