# Limiting the rotation of the camera

,Here’s my code

``````public class NewBehaviourScript : MonoBehaviour
{
bool KeyW = false;
bool KeyD = false;
bool KeyA = false;
bool KeyS = false;
float MouseX;
float MouseY;
public int camSens = 10;
public Transform camtransform;
// Update is called once per frame
void Update()
{
keyDetection();
MouseMovement();
}
void keyDetection()
{
MouseX = Input.GetAxis("Mouse X");
MouseY = Input.GetAxis("Mouse Y");
}
void MouseMovement()
{
//looking in the Y
transform.localEulerAngles = transform.localEulerAngles + new Vector3(0, MouseX * camSens, 0);
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//looking in the X
camtransform.localEulerAngles = camtransform.localEulerAngles + new Vector3(MouseY * camSens, 0, 0);
}
``````

What im trying to do is limiting the camera movement in the x axis.

This was so hard for me to understand at first but I’ll try to show it to you as best as I can

``````float MouseX;
public float rotationSpeed = 1f;
public float xRotation; //This will be the value of your x rotation on the Transform
public float minimum;
public float maximum;

void Update()
{
xRotation = Mathf.Clamp(xRotation, minimum, maximum); //The float value for the camera's rotation will be set to this value, so we clamp it in between a minimum and maximum amount

xRotation += MouseX * rotationSpeed;// So now your x rotation will increase/decrease whenever you move your mouse at the rate of rotationSpeed

transform.rotation = Quaternion.Euler(0, xRotation, 0);// You have to literally set your rotation equal to different values. They can either be variables or set values
}
``````

Uncommented version below
*
float MouseX;
public float rotationSpeed = 1f;
public float xRotation;
public float minimum;
public float maximum;

``````void Update()
{
xRotation = Mathf.Clamp(xRotation, minimum, maximum);

xRotation += MouseX * rotationSpeed;

transform.rotation = Quaternion.Euler(0, xRotation, 0);
}
``````