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

Restrict camera rotation

Discussion in 'Scripting' started by Zoford, Mar 24, 2018.

  1. Zoford

    Zoford

    Joined:
    Mar 1, 2017
    Posts:
    13
    How can I make the camera stop before going beyond a certain point.
    for example I don't want it to be able to make a full 180 degree turn on the Y axis.

    Code (CSharp):
    1. void MouseLook()
    2.     {
    3.         float _mouseX = Input.GetAxis("Mouse X");
    4.         float _mouseY = Input.GetAxis("Mouse Y");
    5.  
    6.         Vector3 newRotation = transform.localEulerAngles;
    7.         Vector3 newCamRotaion = _playerCam.transform.localEulerAngles;
    8.         newRotation.y += _mouseX * _sensitivity;
    9.         newCamRotaion.x += _mouseY*-1* _sensitivity;
    10.         transform.localEulerAngles = newRotation;
    11.         _playerCam.transform.localEulerAngles = newCamRotaion;
    12.     }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Code (csharp):
    1.  
    2. newRotation.y += _mouseX * _sensitivity;
    3. newRotation.y = Mathf.Clamp(newRotation.y,-90,90);
    4.  
     
  3. Zoford

    Zoford

    Joined:
    Mar 1, 2017
    Posts:
    13
    This should work but every time I try to look up it snaps the camera to the min rotation.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Try like this, instead of fussing with euler angles:
    Code (csharp):
    1. float yRotation = 0;
    2.  
    3. void Update() {
    4.    float _mouseX = Input.GetAxis("Mouse X");
    5.    // your other, unrelated code
    6.    yRotation += _mouseX * _sensitivity;
    7.    yRotation = Mathf.Clamp(yRotation, -90, 90);
    8.    transform.rotation = Quaternion.Euler(0,yRotation,0);
    9.  }
    Maybe you want the camera and not the player. Maybe you want both, I wasn't sure.. but the idea is the same.
     
    thomaskoji, DGulyasar and Zoford like this.
  5. Zoford

    Zoford

    Joined:
    Mar 1, 2017
    Posts:
    13
    fire7side's anwser was correct but when I apply it I can't rotate the camera to a negative rotation.
    Once the x axis reaches 0 on the camera it snaps to 90 degrees again.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Do something like this:
    Code (csharp):
    1.  
    2.    newRotation.y += _mouseX * _sensitivity;
    3.   Debug.Log(newRotation.y);
    4. //  newRotation.y = Mathf.Clamp(newRotation.y,-90,90);
    5.  
    6.  
    See what the numbers are when you go to the left and right. It depends where zero is, so make sure if the numbers stop and start at 360, you know where that is because it might take a different kind of statement. Usually that works.
    Also, at least try methos5k code. That might take care of it.
    Otherwise, I think you are facing zero and the numbers decrease from 360.
     
    Last edited: Mar 25, 2018
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You definitely want to store and explicitly set your rotation as methos5k's code does or you're going to run into gimbal lock / euler angle issues because different values can represent the same rotation. How exactly does your camera move in the game world?
     
  8. Zoford

    Zoford

    Joined:
    Mar 1, 2017
    Posts:
    13
    I tried methos5k's code but it didn't work so I erased everything and rewrote it with his code but still it doesn't work.
    This time I cant move the camera at all, it keeps snapping back to the centre of the screen.

    Code (CSharp):
    1. void MouseLook()
    2. {
    3.         float _mouseX = Input.GetAxis("Mouse X");
    4.         float _mouseY = Input.GetAxis("Mouse Y");
    5.  
    6.         var newRotation = transform.rotation;
    7.         newRotation.y += _mouseX * _sensitivity;
    8.         Debug.Log(newRotation.y);
    9.         transform.rotation = Quaternion.Euler (0, newRotation.y, 0);
    10.  
    11.         var newCamRotation = _playerCam.transform.rotation;
    12.         newCamRotation.x += _mouseY * _sensitivity;
    13.         newCamRotation.x = Mathf.Clamp (newCamRotation.x, -90, 90);
    14.         _playerCam.transform.rotation = Quaternion.Euler (newCamRotation.x, 0, 0);
    15.  }
    The script is attached to the player(capsule), and the camera is a child of the player.
    The MouseLook method is called in the Update method.
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Try commenting out the Mathf.Clamp line.
     
  10. Typods

    Typods

    Joined:
    Feb 27, 2018
    Posts:
    55
    VerditerWolf, I ran into this problem quite a while ago and saw a video that explained it to me, however the video is very long and I don't think you need me to explain it but here is the script and the directions to use it...


    Im not sure if the script I included in here worked as I'm farely new to this so I will copy and paste the insides of it.

    make sure to replace PutScriptNameHere with the name of the script you place this in.
    As well as that, a good mouse sensitivity for me is around 10 and if this is centered around a specific object or player, make sure the camera is slightly above the player/object and is facing the same way the player is. Then, make the camera the child of the player/object and assign the player/object to the playerBody Transform.

    NOTE: when you rotate the camera it will cause the player to rotate as well.

    This script is good for FPS games but can be configured rather easily. Just make sure the camera is not starting off facing directly up or down and if the controls are inverted, try rotating the camera. Good Luck!



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PutScriptNameHere : MonoBehaviour
    {
    public Transform playerBody;
    public float mouseSensitivity;
    float xAxisClamp = 0.0f;
    void Awake()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }
    void Update()
    {
    RotateCamera();
    }

    void RotateCamera()
    {
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");

    float rotAmountX = mouseX * mouseSensitivity / 2;
    float rotAmountY = mouseY * mouseSensitivity / 2;
    xAxisClamp -= rotAmountY;
    Vector3 targetRotCam = transform.rotation.eulerAngles;
    Vector3 targetRotBody = playerBody.rotation.eulerAngles;
    targetRotCam.x -= rotAmountY;
    targetRotCam.z = 0;
    targetRotBody.y += rotAmountX;
    if (xAxisClamp > 90)
    {
    xAxisClamp = 90;
    targetRotCam.x = 90;
    }
    else if (xAxisClamp < -90)
    {
    xAxisClamp = -90;
    targetRotCam.x = 270;
    }
    transform.rotation = Quaternion.Euler(targetRotCam);
    playerBody.rotation = Quaternion.Euler(targetRotBody);
    }
    }
     

    Attached Files:

  11. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    OK, well, if you still want to use your code, I it works like this:
    Code (csharp):
    1.  
    2.             float _mouseX = Input.GetAxis("Mouse X");
    3.             float _mouseY = Input.GetAxis("Mouse Y");
    4.  
    5.             var newRotation = transform.rotation.eulerAngles;
    6.             newRotation.y += _mouseX * _sensitivity;
    7.            // Debug.Log(newRotation.y);
    8.         if (newRotation.y < 180 && newRotation.y > 0)
    9.         {
    10.            newRotation.y = Mathf.Clamp(newRotation.y, 0, 90);
    11.         }
    12.         else if(newRotation.y < 360 && newRotation.y > 180)
    13.         {
    14.             newRotation.y = Mathf.Clamp(newRotation.y, 270, 360);
    15.         }
    16.           transform.rotation = Quaternion.Euler(0, newRotation.y, 0);
    17.  
    18.  
    19.             var newCamRotation = _playerCam.transform.rotation.eulerAngles;
    20.             newCamRotation.x += _mouseY * _sensitivity;
    21.  
    22.           _playerCam.transform.rotation = Quaternion.Euler(newCamRotation.x, newRotation.y, 0);
    23.  
     
    Last edited: Mar 25, 2018
    Zoford likes this.
  12. Zoford

    Zoford

    Joined:
    Mar 1, 2017
    Posts:
    13
    Yes this works thanks but you made some errors the code should look like this:
    Code (CSharp):
    1. float _mouseX = Input.GetAxis("Mouse X");
    2.         float _mouseY = Input.GetAxis("Mouse Y");
    3.  
    4.         var newRotation = transform.rotation.eulerAngles;
    5.         newRotation.y += _mouseX * _sensitivity;
    6.         transform.rotation = Quaternion.Euler(0, newRotation.y, 0);
    7.  
    8.  
    9.         var newCamRotation = _playerCam.transform.rotation.eulerAngles;
    10.         newCamRotation.x -= _mouseY * _sensitivity;
    11.         if (newCamRotation.x < 180 && newCamRotation.x > 0)
    12.         {
    13.             newCamRotation.x = Mathf.Clamp(newCamRotation.x, 0, 90);
    14.         }
    15.         else if(newCamRotation.x < 360 && newCamRotation.x > 180)
    16.         {
    17.             newCamRotation.x = Mathf.Clamp(newCamRotation.x, 270, 360);
    18.         }
    19.         _playerCam.transform.rotation = Quaternion.Euler(newCamRotation.x, newRotation.y, 0);
    Thanks to everyone that responded.
     
  13. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Glad you got it working.
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just for the record, when you tried to add my code, you did not do it correctly. You were creating local variables (and of a quaternion). My example was using a float variable, stored at the class level, which you would modify based on the "Mouse X" input, and then use it as part of creating a rotation. :)
     
  15. Zoford

    Zoford

    Joined:
    Mar 1, 2017
    Posts:
    13
    Ok thanks, but you still helped me in the end.