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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How Do I Clamp Rotation?

Discussion in 'Scripting' started by UnknownGameDev123, Apr 10, 2020.

  1. UnknownGameDev123

    UnknownGameDev123

    Joined:
    Nov 17, 2019
    Posts:
    5
    I had been developing a 3D First - Person game. I programmed the mouse look of the player and it is working great, except one thing. I was not able to clamp the x rotation variable, so that my player doesn't go on looking up. Mathf.clamp or if statement doesn't seem to work :-\

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseLook : MonoBehaviour
    4. {
    5.      public Transform player;
    6.    
    7.      public float sensitivity = 20f;
    8.      private float hAxis, vAxis;
    9.      private float yRot, xRot;
    10.    
    11.      // Start is called before the first frame update
    12.      void Start()
    13.      {
    14.          Cursor.lockState = CursorLockMode.Locked;
    15.      }
    16.      // Update is called once per frame
    17.      void Update()
    18.      {
    19.          hAxis = Input.GetAxisRaw("Mouse X");
    20.          vAxis = Input.GetAxisRaw("Mouse Y");
    21.          yRot = sensitivity * hAxis;
    22.          xRot = sensitivity * vAxis;
    23.          if(xRot < -90f)
    24.          {
    25.              xRot = -90f;
    26.          } else if(xRot > 90f)
    27.          {
    28.              xRot = 90f;
    29.          }
    30.          //Mathf.Clamp(xRot, 0f, 0f);
    31.          player.Rotate(0f, yRot, 0f);
    32.          transform.Rotate(-xRot, 0f, 0f);
    33.      }
    34. }
    Please help me.... I am struggling
     
  2. LatchGameDev

    LatchGameDev

    Joined:
    Nov 24, 2014
    Posts:
    61
    I'm wondering if you were using Mathf.Clamp correctly based on your comment on line 30.

    Code (CSharp):
    1. xRot = Mathf.Clamp(xRot, 0f, 0f);
    It's a function that returns the result of the Clamp so to properly use it you need to write the line like that.
    With that fix in you'll still not be limiting the rotation since transform.Rotate is like saying "Apply a rotation of 10 units." It's not a "Set the rotation to angle 10"

    Also if you put a debug line in you'd see that xRot is not getting larger or smaller. Each frame you are setting xRot to be sensitivty * hAxis; That at most will be your sensitivity when hAxis is at it's max of 1;
    Check the value for yourself with Debug.Log("Value is "+xRot); right before your if statements

    Check out the unity docs to see how these engine functions / variables are meant to be used.
    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
    https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Transform.html

    Good luck and happy deving.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CamIssue2_MouseLook : MonoBehaviour
    4. {
    5.     public Transform player;
    6.     public float minX = -45;
    7.     public float maxX = 45;
    8.  
    9.     public float xSensitivity = 200f;
    10.     public float ySensitivity = 200f;
    11.     private float vAxis;
    12.     private float xAngle = 0;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         Cursor.lockState = CursorLockMode.Locked;
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         // Get the axis input -1 to 1 multiple by sensitivity and then multiply by time
    23.         // to have this be the same speed independent of FPS
    24.         // Also notice the += means we are adding to that value
    25.         xAngle += Input.GetAxisRaw("Mouse X") * xSensitivity * Time.deltaTime;
    26.         // Clamp it
    27.         xAngle = Mathf.Clamp(xAngle,minX,maxX);
    28.  
    29.         // Set the rotation
    30.         transform.rotation = Quaternion.Euler(-xAngle, 0f, 0f);
    31.  
    32.         // The below code hasn't been touched, just moved except for adding Time.deltaTime
    33.         vAxis = Input.GetAxisRaw("Mouse Y") * ySensitivity * Time.deltaTime;
    34.         player.Rotate(0f, vAxis, 0f);
    35.  
    36.     }
    37. }
     
    Last edited: Apr 10, 2020
  3. UnknownGameDev123

    UnknownGameDev123

    Joined:
    Nov 17, 2019
    Posts:
    5
    Oops, to show that I have used mathf.clamp I had wrote that comment. Guess I wrote that wrong. Now I realize that I wrote that wrong. Thanks for your help! Unfortunately the correct clamp doesn't work either. I don't know why. Could you please help?
     
  4. UnknownGameDev123

    UnknownGameDev123

    Joined:
    Nov 17, 2019
    Posts:
    5
    okay! Now i understand the problem! If we look at my code closely, we find that xRot can never be more that 90 or less that -90 because we are getting the value by multiplying sensitivity and vAxis. Now the question is how do we fix this?
     
  5. LatchGameDev

    LatchGameDev

    Joined:
    Nov 24, 2014
    Posts:
    61
    Did you copy my script and put it in the game and your still having issues?

    Assuming you modified your original with the correct use of Mathf.Clamp you would still be having issues as I stated before.

    "With that fix in you'll still not be limiting the rotation since transform.Rotate is like saying "Apply a rotation of 10 units." It's not a "Set the rotation to angle 10"

    Look at my code, i'm not using Transform.Rotate
     
  6. LatchGameDev

    LatchGameDev

    Joined:
    Nov 24, 2014
    Posts:
    61
    The way your using xRot it's more of how much should we rotate this frame? Again not setting the rotation to an exact value. So it won't matter if you clamp xRot, your still saying rotate a little bit each frame.
     
  7. UnknownGameDev123

    UnknownGameDev123

    Joined:
    Nov 17, 2019
    Posts:
    5
    Wow... It works! First I had only pasted the correct Mathf.Clamp() code. But now when I pasted your whole code it is working awesome. Thanks a very lot.