- Home /
 
how do I limit camera vertical rotation
can you please help me with limiting camera's vertical rotation with my code? heres the code:
public float speed = 5, sensetivity;
private PlayerMoto motor;
public GameObject hands;
public camera cam;
// Start is called before the first frame update
void Start() { motor = GetComponent(); cam = GetComponent(); } // Update is called once per frame void Update() { //cam.transform.localEulerAngles = new Vector3(cam.transform.localEulerAngles.x, 0, 0); float _xmove = Input.GetAxis("Horizontal"); float _zmove = Input.GetAxis("Vertical");
  Vector3 _moveHorizontal = transform.right * _xmove;
  Vector3 _movVertical = transform.forward * _zmove;
  Vector3 _velocity = (_moveHorizontal + _movVertical) * speed;
  motor.Move(_velocity);
  float _yRot = Input.GetAxis("Mouse X");
  Vector3 _rotation = new Vector3(0f, _yRot, 0f) * sensetivity;
  motor.Rotate(_rotation);
  float _xRot = Input.GetAxis("Mouse Y");
  Vector3 _cameraRotation = new Vector3(_xRot, 0f, 0f) * sensetivity;
  motor.RotateCamera(_cameraRotation);
 
  if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) {
      for (; speed <= 15; speed++)
      {
          speed = 15;
      }
  }
  else
  {
      speed = 10;
  }
 
               }
Answer by SpaghettiCloud · Aug 10, 2019 at 09:47 AM
You could implement a clamping mechanism similar to this one:
  public class PlayerLook : MonoBehaviour
 {
     [SerializeField] private string mouseXInputName, mouseYInputName; 
     [SerializeField] private float mouseSensitivity;
 
     [SerializeField] private Transform playerBody;
 
     private float xAxisClamp;
 
     private void Awake()
     {
         LockCursor();
         xAxisClamp = 0.0f;
     }
 
 
     private void LockCursor()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     private void Update()
     {
         CameraRotation();
     }
 
     private void CameraRotation()
     {
         float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
 
         xAxisClamp += mouseY;
 
         if(xAxisClamp > 90.0f)
         {
             xAxisClamp = 90.0f;
             mouseY = 0.0f;
             ClampXAxisRotationToValue(270.0f);
         }
         else if (xAxisClamp < -90.0f)
         {
             xAxisClamp = -90.0f;
             mouseY = 0.0f;
             ClampXAxisRotationToValue(90.0f);
         }
 
         transform.Rotate(Vector3.left * mouseY);
         playerBody.Rotate(Vector3.up * mouseX);
     }
 
     private void ClampXAxisRotationToValue(float value)
     {
         Vector3 eulerRotation = transform.eulerAngles;
         eulerRotation.x = value;
         transform.eulerAngles = eulerRotation;
     }
 }
 
               I found this code here, please visits its creator's github page [Acacia-Developer][1]
 [1]: https://github.com/Acacia-Developer 
nothing working Im trying the best as I can but I still fail
Your answer