x


Limiting the rotation of an object

I'm using a Object (Cannon) that rotates only in 2 axis. And I'm trying to limit the rotation that this Cannon can do.

My object start with all Rotation in 0. And I want the rotation is limited from 0 to -90 (270).

=> This object cant rotate to angles > (more than) 0, and < (less than) 270 (or -90)

I mean: (angle < 0 && angle > 270)

A little CODE:

    transform.rotation = Quaternion.LookRotation(inputRotation); // inputRotation get the mouse position (x, z). Z get the value of Y.      
    transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.y + 0);//-transform.eulerAngles.y + 0);

And I found this code to limit the rotation, but when I put the mouse in a position that give a angle more than 0, the object back to the value -90. If the object stayed in the position 0 when angle were more than 0, and stayed in -90 (or 270) when the angle were less than 270 will fixed.

The function to limit:

function LateUpdate()
{
     transform.rotation.eulerAngles = new Vector3(
          transform.rotation.eulerAngles.x,
          transform.rotation.eulerAngles.y,
          Mathf.Clamp(transform.rotation.eulerAngles.z, 270, 360)
     );
}

Thks for all!

more ▼

asked Dec 17 '10 at 11:18 PM

HIT gravatar image

HIT
13 2 2 6

2 days and nothing...someone ?

Dec 20 '10 at 11:03 PM HIT
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

I had a similar problem (see here) that was solved by using transform.localEulerAngles instead.

more ▼

answered Mar 10 '11 at 11:20 PM

fetimo gravatar image

fetimo
27 1 1 4

(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:

x2244
x1070
x137
x57

asked: Dec 17 '10 at 11:18 PM

Seen: 1153 times

Last Updated: Dec 17 '10 at 11:18 PM