# How to set maximum and minimum rotation for an object

 0 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! more ▼ asked Oct 30 '10 at 01:46 PM Taavi Uotila 26 ● 3 ● 3 ● 7 Does the object only rotate on the x axis? Oct 30 '10 at 02:14 PM Jesse Anders add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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. more ▼ answered Oct 30 '10 at 02:48 PM duck ♦♦ 41k ● 92 ● 148 ● 415 transform.rotation is a quaternion...but you know that. :) Oct 30 '10 at 03:56 PM Eric5h5 oops, of course! bad example - fixing now Oct 30 '10 at 06:40 PM duck ♦♦ fixed. for all relevant disclaimers, please see my profile page ;-) Oct 30 '10 at 06:48 PM duck ♦♦ add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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. more ▼ answered Oct 30 '10 at 03:59 PM Eric5h5 80.3k ● 42 ● 132 ● 521 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3460
x2166
x1036
x34
x29

asked: Oct 30 '10 at 01:46 PM

Seen: 1958 times

Last Updated: Oct 30 '10 at 01:46 PM