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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[SOLVED] How to clamp Camera rotation on the X Axis - FPS controller

Discussion in 'Scripting' started by Rocheofpower, Apr 15, 2018.

  1. Rocheofpower

    Rocheofpower

    Joined:
    Feb 5, 2018
    Posts:
    4
    Hi all,

    Have been trying for hours to crack this, and can't seem to make reasonable headway.
    I'm making a first person shooter and want to restrict how far the player can look up or down - as it is the player can rotate forever on the X-axis, as if he were somersaulting.
    I've been attempting different things with the MathF.Clamp method and Quaternion.Euler() but to no avail.

    the script is attached to a Capsule, and the Camera is a child of the capsule.
    Here is my current code to move the player:
    Code (CSharp):
    1. public class InputManager : MonoBehaviour {
    2.  
    3.  
    4.  
    5.     //Here I declare the speed of movement and speed of rotation
    6.     public float speed = 1f;
    7.     public float RotSpeed = 6f;
    8.     //Here I declare two gameobjects which I've assigned in the inspector
    9.     public GameObject Camera;
    10.     public GameObject PauseMenu;
    11.     public GameObject Player;
    12.     //this boolean to to tell if game should be paused or not
    13.     public static bool GamePaused;
    14.     //Boolean to detect if player is grounded
    15.     public bool Grounded;
    16.     //movement forward-backwards
    17.     float MoveFB;
    18.     //movement left-right
    19.     float MoveLR;
    20.     //rotation on x and y axis
    21.     float rotX;
    22.     float rotY;
    23.  
    24. void Update () {
    25.         //first declare a float to hold the value of the virtual axis
    26.         //and multiply it by the speed
    27.         if (GamePaused == false)
    28.         {
    29.  
    30.         MoveFB = Input.GetAxis ("Vertical")*speed;
    31.         //use transform.translate to then move the player along that axis
    32.         //at that speed
    33.         transform.Translate (0, 0, MoveFB);
    34.         MoveLR = Input.GetAxis ("Horizontal") * speed;
    35.         transform.Translate(MoveLR,0,0);
    36.  
    37.         //now for the mouse rotation
    38.         rotX = Input.GetAxis("Mouse X")*RotSpeed;
    39.         rotY = Input.GetAxis ("Mouse Y")*RotSpeed;
    40.        
    41.          
    42.  
    43.         //Camera rotation only allowed if game us not paused
    44.             Camera.transform.Rotate(-rotY, 0, 0);
    45.             transform.Rotate(0, rotX, 0);
    46.  
    47.         }
    I've also tried attaching a script to the camera object and restricting it's movement in this way:
    Code (CSharp):
    1.   private float InputX;
    2.     private float InputY;
    3.     private float Yrotation;
    4.     private float Xrotation;
    5.     public float RotationSpeed = 7f;
    6.     private GameObject Camera;
    7.     // Use this for initialization
    8.     void Start () {
    9.         Camera = this.gameObject;
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         InputX = Input.GetAxis("Mouse X")*RotationSpeed;
    15.         InputY = Input.GetAxis("Mouse Y")*RotationSpeed;
    16.        // Yrotation = InputY;
    17.         Yrotation += InputY;
    18.         Xrotation += InputX;
    19.         Yrotation = Mathf.Clamp(Yrotation, -90f, 90f);
    20.         transform.rotation = Quaternion.Euler(-Yrotation, transform.parent.rotation.y,
    21.             transform.parent.rotation.z);
    22.        // transform.parent.Rotate(0f, Xrotation, 0f);
    This restricted the camera's axis exactly how I wanted but kept snapping it's orientation to the back of the player's head, and no rotation on the Y axis was possible.

    Any advice you might have on restricting the camera on the x axis is greatly appreciated :)
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Simple way is to store your own values and rebuild your rotations every frame rather than accumulating them as you're doing now.

    Code (csharp):
    1.  
    2.         //now for the mouse rotation
    3.         rotX += Input.GetAxis("Mouse X")*RotSpeed;
    4.         rotY += Input.GetAxis ("Mouse Y")*RotSpeed;
    5.  
    6.    rotY = Mathf.Clamp(rotY, -90f, 90f);      
    7.  
    8.         //Camera rotation only allowed if game us not paused
    9.    Camera.transform.rotation = Quaternion.Euler(-rotY, 0f, 0f);
    10.    transform.rotation = Quaternion.Euler(0f, rotX, 0f);
    11.  
     
  3. Rocheofpower

    Rocheofpower

    Joined:
    Feb 5, 2018
    Posts:
    4
    Muchas Gracias my friend, really helped me out :)
    It was still locking the camera's orientation to look out the back of the player's head, but restricted the X axis movement perfectly (seems to be I had to access the local rotation)
    So I added the local rotation to your code and now it seems to work stellar:
    Code (CSharp):
    1.  
    2.             rotX += Input.GetAxis("Mouse X")*RotSpeed;
    3.             rotY += Input.GetAxis("Mouse Y") * RotSpeed;
    4.             rotY = Mathf.Clamp(rotY, -90f, 90f);
    5.             Camera.transform.localRotation = Quaternion.Euler(-rotY, 0f, 0f);
    6.             transform.rotation = Quaternion.Euler(0f, rotX, 0f);
    Thanks again for the help mate!
     
    screret, eminabbasov1305 and GroZZleR like this.
  4. Banshee509

    Banshee509

    Joined:
    Feb 11, 2021
    Posts:
    1
    I saw this on github somewhere. "Hippity Hoppity your code is now my property"

    Thanks friend.
     
  5. aakashsolanki890

    aakashsolanki890

    Joined:
    Jan 13, 2021
    Posts:
    3
    Here is a Easy way of clamping camera rotation
     
    Hannah22milne likes this.
  6. AHMED59762

    AHMED59762

    Joined:
    Feb 26, 2021
    Posts:
    2
    Hi guys how do I make my camera move in the Joystick
     
  7. VLRSOFT

    VLRSOFT

    Joined:
    Feb 29, 2020
    Posts:
    12
    Thanks! it came in handy
     
  8. Deleted User

    Deleted User

    Guest

    If you want to be finicky, but technically correct, use
    rotX = (rotX + Input.GetAxis("Mouse X")*RotSpeed)%360
    and
    rotY = (rotY + Input.GetAxis ("Mouse Y")*RotSpeed)%360
    to prevent over/underflow.