how do I limit the angle that a bone can move using quaternions?

I have an angle I want to limit for a bone of an object the user moves the bone with the mouse.

Here’s an example
limit=Vector3(45,45,90);

in an update loop I have:

if(bone.transform.eulerAngles.x>=limit.x){
bone.transform.eulerAngles.x=limit.x
}

Using this type of code I get flipping and vibrating of the object. If I debug.log out the values for euler angles they are also jumping around.

Someone in another post suggested using modulus

if(bone.transform.eulerAngles.x%360>=limit.x){
bone.transform.eulerAngles.x=limit.x;
}

but I tried that and it still has the flipping vibrating.

Does anyone have suggestions on how to limit rotations using quaternions instead of euler angles.

Dan

A characterJoint might be easiest – it works like a ball&socket with limits on up/down and side-side motion.

Setting one Euler angle of a quat gives randomish results (see the docs.) That flipping is normal and not very fixable. In general, you have to use either Eulers or Quats as the “base” and can’t go back and forth. It’s a little like translating from Latin to English, changing one word, then translating back. You can get something really different.

You can check the overall “cone” angle of a Quaternion, and can probably scale it down if it gets too big, but I don’t know how to limit the angle to an oval instead of a ring.

You could have the user control the Y and X rotation, keep them saved, and build Quaternion.EulerAngles(X,Y,0)' after each change. Or you could have the user select a point only from within a small ring in front of the bone, and use that point for a lookAt.

just look at the camera control mouse look and then yuo’ll find how to properly use Mathf.Clamp :smiley: