# How to Mathf.Clamp x Rotation on Camera?

I’ve been at this longer than I care to admit and I need help. I got the code to work, except whenever I drag the camera down, the camera teleports back to max value. (40f). I found out it’s because x Rotation will not go to -40f, instead it changes into 360. How do I fix this?

``````//Clamps didn't work because Rot X would turn into 360 instead of -40f.

behaveList[1].transform.localEulerAngles = new Vector3(
Mathf.Clamp(behaveList[1].transform.localEulerAngles.x,
-40f, 40f), 0, 0);

//Motion
if (Input.GetAxis("Mouse Y") > .1f)
{
behaveList[1].transform.Rotate(new Vector3(70f, 0, 0) * Time.deltaTime, Space.Self);
}
if (Input.GetAxis("Mouse Y") < -.1f)
{
behaveList[1].transform.Rotate(new Vector3(-70f, 0, 0) * Time.deltaTime, Space.Self);
}
``````

``````&& behaveList[1].transform.localEulerAngles.x > 80

&& behaveList[1].transform.localEulerAngles.x < 270
``````

so it only rotates more than 80 degrees and less than 270 degrees. or x degrees you looking for.

Also when you do a -40, it’s the same as 320 degrees. because degrees are only 0 to 360.

Or if you still want to use a clamp , i’ve found the code below works well for angles from -180 to +180. Mathf.clamp is just a set of if statements anyway:

``````// this assumes your clamping eulerAngle (0->360)
// but will clamp on a -180 to 180 value
// then return the correct 0 to 360 value
float AngleClamp(float angle, float min, float max)
{

if (angle > 180)
angle -=360;
angle = Mathf.Max(Mathf.Min(angle,max),min);
if (angle<0)
angle+=360;
return angle;
}
``````