x


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!

more ▼

asked Oct 30 '10 at 01:46 PM

Taavi Uotila gravatar image

Taavi Uotila
26 3 3 7

Does the object only rotate on the x axis?

Oct 30 '10 at 02:14 PM Jesse Anders
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 gravatar image

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 ♦♦
(comments are locked)
10|3000 characters needed characters left

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 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

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