- Home /
How to set maximum and minimum rotation for an object
Hi! I'm trying to make a 2d cannon game, but I've faced a problem. I should limit the x-rotation of that object between 10 and 80. Thank You!
Answer by duck · Oct 30, 2010 at 02:48 PM
This would depend on how you're rotating the object in the first place. If you're using physics and torque, a configurable joint would fit well.
If you're rotating by manipulating the transform directly, without physics, it's probably best to maintain a variable containing the angle, and limit it yourself, perhaps using the Mathf.Clamp function. For example:
var angle = 45.0;
function Update() {
angle += Input.GetAxis("Vertical") * Time.deltaTime * 10;
angle = Mathf.Clamp( angle, 10, 80 );
transform.localRotation = Quaternion.AngleAxis(angle, Vector3.right);
}
If you need a more detailed answer, please edit your question to include more detail about how you're rotating the object.
transform.rotation is a quaternion...but you know that. :)
fixed. for all relevant disclaimers, please see my profile page ;-)
Answer by Eric5h5 · Oct 30, 2010 at 03:59 PM
Have a look at the MouseLook script in Standard Assets, which has a rotation clamp function that would work perfectly in this situation as long as you're directly setting the rotation and not using physics. If you need a Javascript version of MouseLook.cs, it's here.