# How to clamp Y rotation of camera?

Hi! So, i am trying to make a third person shooter and i have the movement/ camera script done, but i have one little problem. When i press right-click to aim, i cant clamp the y rotation of the camera and i can rotate the camera 360 degrees. Also the values are weird, when the camera goes up the x descends, and when the camera goes down the x value gets bigger.

Here is the script:

private const float Y_ANGLE_MIN = 0f;
private const float Y_ANGLE_MAX = 50f;

``````public Transform lookAt;
public Transform cameraTransform;
public Transform shootPoint;
public float rotationSpeed;
public bool isShooting;

private Camera cam;

private float distance = 5f;
private float currX = 0f;
private float currY = 0f;
private float mouseX = 0f;
private float mouseY = 0f;

private void Start()
{
Cursor.lockState = CursorLockMode.Locked;

cameraTransform = transform;
cam = Camera.main;
}

private void Update()
{
currX += Input.GetAxis("Mouse X") * rotationSpeed;
currY += Input.GetAxis("Mouse Y") * rotationSpeed * -1f;

currY = Mathf.Clamp(currY, Y_ANGLE_MIN, Y_ANGLE_MAX);

if(Input.GetKey(KeyCode.Mouse1))
{
if(isShooting == false)
{
cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
}
isShooting = true;
cameraTransform.position = shootPoint.position;

mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
lookAt.Rotate(mouseX * Vector3.up);

mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
}
if(Input.GetKeyUp(KeyCode.Mouse1))
{
isShooting = false;
}
}

private void FixedUpdate()
{
if (isShooting == false)
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currY, currX, 0);
cameraTransform.position = lookAt.position + rotation * dir;
cameraTransform.LookAt(lookAt.position);
}
}
``````

I would be glad if someone could help me out :)!

public Transform lookAt;
public Transform cameraTransform;
public Transform shootPoint;
public float rotationSpeed;
public float lockAngle = 45;
public bool isShooting;
private Camera cam;
private float distance = 5f;
private float currX = 0f;
private float currY = 0f;
private float mouseX = 0f;
private float mouseY = 0f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
cameraTransform = transform;
cam = Camera.main;
}
private void Update()
{
currX += Input.GetAxis(“Mouse X”) * rotationSpeed;
currY += Input.GetAxis(“Mouse Y”) * rotationSpeed * -1f;
currY = Mathf.Clamp(currY, Y_ANGLE_MIN, Y_ANGLE_MAX);
if(Input.GetKey(KeyCode.Mouse1))
{
if(isShooting == false)
{
cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
}
isShooting = true;
cameraTransform.position = shootPoint.position;
mouseX = Input.GetAxis(“Mouse X”) * rotationSpeed;
cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
lookAt.Rotate(mouseX * Vector3.up);
mouseY = Input.GetAxis(“Mouse Y”) * rotationSpeed * -1f;
cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
}
if(Input.GetKeyUp(KeyCode.Mouse1))
{
isShooting = false;
}
}
private void FixedUpdate()
{
if (isShooting == false)
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currY, currX, 0);
cameraTransform.position = lookAt.position + rotation * dir;
cameraTransform.LookAt(lookAt.position);
cameraTransform.eulerAngles = new Vector3(cameraTransform.eulerAngles.x,lockAngle,cameraTransform.eulerAngles.z);
}
}
that should the camera’s y rotation…