Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Euler angle clamp help

Discussion in 'Scripting' started by ForrestX, May 29, 2012.

  1. ForrestX

    ForrestX

    Joined:
    Apr 17, 2010
    Posts:
    94
    I need to clamp an angle by -90 +90, but the angle goes from 0 to 360.
    I use "transform.rotation.eulerAngles" to get my angles and the classic ClampAngle function to clamp, but it can't work because the correct angle would go from 270 to 0 and from 0 to 90

    how to solve this?
    (also why sometimes the transform.rotation.eulerAngles returns me an angle that is 0-360 and sometimes -360+360? )

    thx!
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,040
    Add 90, clamp to 0 - 180, subtract 90.
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Depending on what you are trying to do, this might not be correct, but if you are say trying to let someone turn a wheel until it gets to -90 or 90

    Code (csharp):
    1.  
    2. float angleX = transform.rotation.eulerAngles.x;
    3.  
    4. if(angleX > 180  angleX < 270)
    5.  angleX = 270;
    6. else if (angleX < 180  angleX > 90)
    7.  angleX = 90;
    8.  
     
    dman8723 and A_never_kill like this.
  4. ForrestX

    ForrestX

    Joined:
    Apr 17, 2010
    Posts:
    94
    thanks :)
     
  5. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    This helped me, thanks!
     
  6. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    I always do it via helper Vector3. just clamp the helper vector and then set eulerangle=helper...a vector3 doesnt have that angle restrictions