x


Quaternions and cube rotation.

Hello,

here I'm stuck with quaternion usage (I know I'm not alone and that's reassuring... a bit:)).

I have a cube. I want to make this cube rotates from 90 degrees on X axis or Y axis in worldspace.

Here is the code I use in the cube script :

void Update() 
{
    if (transform.rotation == _nextRot)
    {
       _currentRot = transform.rotation;
       if (_onX == false)
       {
         _nextRot = transform.rotation * Quaternion.Euler(transform.up * 90);
         _onX = true;
       }
       else
       {
         _nextRot = transform.rotation * Quaternion.Euler(transform.right * 90);
         _onX = false;
       }
       _turntime = Time.time;         
    }
    transform.rotation = Quaternion.Slerp(_currentRot,_nextRot, (Time.time - _turntime) * 0.2f);
}

As I understand it, using transform.up or transform.right and multiplies it by my rotation gives me a new rotation in worldspace. However this code gives me a cube rotating on X one time then only on Y axis.

Any idea?

more ▼

asked Jun 23 '12 at 10:00 AM

lvictorino gravatar image

lvictorino
654 7 17 20

Victor ... did you check out the LOOKAT command in Unity? it solves many problems! check it out

Jun 23 '12 at 08:20 PM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You are over complicating it :)

  _nextRot = Quaternion.Euler(90,0,0) * transform.rotation;  // Rotate 90 around X

The transform.right etc is messing it up, because it is changing too and you are accounting for the current rotation by using it in your calculation.

more ▼

answered Jun 23 '12 at 10:03 AM

whydoidoit gravatar image

whydoidoit
33.7k 14 24 105

...you again :) Thank you for this quick answer, however it does not work. In fact using this VISUALLY gives me rotation on: Y,Z,X,Z... etc.

Jun 23 '12 at 10:10 AM lvictorino

Ugh - bloody quaternion rotations - sorry you need to do this:

  _nextRot = Quaternion.Euler(90,0,0) * transform.rotation;  // Rotate 90 around X
Jun 23 '12 at 10:11 AM whydoidoit

Always feels unnatural to me :)

Jun 23 '12 at 10:12 AM whydoidoit

Damn! I didn't know that order was important in Quaternion maths. Thank you A LOT.

Jun 23 '12 at 10:14 AM lvictorino

you need to read up on Peano's axioms, Victor

Jun 23 '12 at 10:55 AM Fattie
(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:

x2249
x1336
x459

asked: Jun 23 '12 at 10:00 AM

Seen: 723 times

Last Updated: Jun 23 '12 at 08:20 PM