- Home /
 
Restrict camera rotation in x and y?
How would I restrict the cameras rotation in x and y in this script?
 private void Update()
     {
         //Check if the left mouse button is held.
         if(Input.GetMouseButton(0))
         {
             //Get the current mouse input.
             yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
             pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
         }
 
         //Get the current scroll input.
         scroll -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
         scroll = Mathf.Clamp(scroll, minDistance, maxDistance);
 
         //Update the rotation.
         currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3(pitch, yaw), ref rotationVelocity, 0.07f);
         transform.eulerAngles = currentRotation;
 
         //Update the position.
         currentDistance = Mathf.SmoothDamp(currentDistance, scroll, ref distanceVelocity, 0.1f);
         transform.position = targetTransform.position + transform.forward * -currentDistance;
     }
 
               thanks in advance :)
               Comment
              
 
               
              Answer by Zodiarc · Feb 07, 2018 at 08:18 AM
That's how I made it (from memory and kinda pseudocode):
 if (camera.rotation.eulerAngles.y > maxY) {
    camera.rotation.eulerAngles.y = maxY;
 }
 if (camera.rotation.eulerAngles.y < minY) {
  camera.rotation.eulerAngles.y = minY;
 }
 
              Your answer