Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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,039
    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:
    712
    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