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?

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.