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

how do I fix this Euler angles with mathf.clamp bug

Discussion in 'Scripting' started by mercygamesproduction, Jan 15, 2021.

  1. mercygamesproduction

    mercygamesproduction

    Joined:
    Oct 31, 2020
    Posts:
    2
    Code (CSharp):
    1.  
    2.     private void Look()
    3.     {
    4.         //Make Camera Follow Head
    5.         camera.transform.position = head.position;
    6.         camera.transform.rotation = head.rotation;
    7.  
    8.         Vector2 inLook = look.ReadValue<Vector2>();
    9.         Vector2 abdomenLowerVector = new Vector2();
    10.         abdomenLowerVector.x = -inLook.y;
    11.         abdomenLowerVector.x *= lookSensitivty * Time.deltaTime;
    12.         abdomenLower.Rotate(abdomenLowerVector * lookSensitivty * Time.fixedDeltaTime);
    13.  
    14.         Vector3 abdomenLowerEulerAngle = abdomenLower.rotation.eulerAngles;
    15.         abdomenLowerEulerAngle.x = Mathf.Clamp(abdomenLowerEulerAngle.x, 0, 45);
    16.         abdomenLower.eulerAngles = abdomenLowerEulerAngle;
    17.     }
    18.  
    mathf.Clamp keeps making abdomenLowerEulerAngle.x = 45, every time abdomenLowerEulerAngle.x goes under 0
     
  2. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    69
    are you sure your angles wrap around to negative and not positive?
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Also worth noting that there are multiple euler angle representations for any given Quaternion, so your best bet is avoiding converting back and forth altogether.
     
    PraetorBlue likes this.