clamping rotation

Can someone tell me why this code wont clamp an objects rotation correctly. For some reason the rotation will only accept 0 for clamping as well.

Quaternion pos = transform.rotation;
pos.z = Mathf.Clamp(pos.z, 0, 103.5f);
transform.rotation = pos;

It’s because rotations aren’t the three numbers you see in the Inspector; those are the Euler angle equivalents. A quaternion has four numbers (w,x,y,z), from -1 to 1, which represent the rotation.

Using Euler angles won’t be your best strategy here either, which requires a long explanation that I happen to have written right here. If you can provide a little more context, we could advise you on how to best handle this clamping behavior. My gut feeling says that the technique described in the final section of that article - treating the rotation strictly as output, never input - is what will probably be most useful to you.

1 Like

Thanks Manta for the reply. I skimmed through your article and I will go back and read it fully. But to answer your question I am using a rigid body to rotate an object and am taking that rotation value and adding those numbers to help rotate another object.

Right now I want to clamp that object with the rigid body to stop rotating after a certain degree.

If this is basically a 2D rotation, then you can use the Atan2 function (described in the article) to work out its angle as needed.

Thanks for the response. After reading your article this is what i came up with.
– update()
Quaternion pos = transform.rotation;
Vector3 values = pos.eulerAngles;
float valuesz = values.z;
float newvalue = Mathf.Clamp(valuesz, 0, 100);

values.z = newvalue;
pos.eulerAngles = values;
transform.rotation = pos;

When I tried using the atan the numbers were not getting higher than 1. Honestly I am very poor at math at the moment and dont know how i could convert that to degrees.

With the code posted I have the clamp working on the max value but not the min. Also when the rotation of the object goes past the min clamp with this code the object starts endlessly rotating but does so in a motion like of 15 degree increments.

Any thoughts on what i am doing wrong?

You just need to multiply it by Mathf.Rad2Deg.

Your second issue is very likely related to using Euler angles, so focus on getting the Atan2 solution working.

If that doesn’t resolve the other issue, try using something like:

Debug.LogFormat("valuesz = {0}   newvalue = {1}", valuesz, newvalue);

To get some insight into what’s going wrong.

Star I tried to do as you recommended and I was still getting problems.

This however does work and doesn’t make the gameobject spaz out. However when I use the code below in values say 90 and 0 the 90 clamp works but the 0 does not and the infinite spinning occurs. Does anyone know why 300 to 45 works but not 90 to 0?

void Update () {
        if (transform.localEulerAngles.y > 300)
        {
            transform.localEulerAngles = new Vector3(0, 300, 0);
        }
        if (transform.localEulerAngles.y < 45)
        {
            transform.localEulerAngles = new Vector3(0, 45, 0);
        }
    }

Because Euler angles suck, plain and simple. Check that article I linked in my first comment.