# Clamping between everything but the min/max values

I have an object, and i want to clamp the z value between -90, and 90. So it has a total of 180 degrees it can go. This is my problem, when in play mode, it is no longer -90, and it is 270. So i need to clamp my angle from 90, to 270, mathematically this is from 90 to 270, but i want it to include 0 to 90, and 270 to 360 its hard to explain so i made a picture right here : http://puu.sh/iB8Lo/f18dede505.png

here is my code in case i’m doing something wrong

``````Quaternion clamp = Quaternion.Euler(transform.localEulerAngles.x, transform.localEulerAngles.y, Mathf.Clamp(transform.localEulerAngles.z, -90, 90));
transform.rotation = clamp;
``````

Haven’t tested this, but it might/should work? The basic idea is that if z <= 180 degrees then use the number as is, otherwise subtract the angle from 360 and use the negative - e.g. -(360-270) = -90. Then clamp between -90 and 90.

``````float z = transform.localEulerAngles.z;
z = Mathf.Clamp((z <= 180) ? z : -(360 - z), -90, 90);
``````