0
\$\begingroup\$

I am having trouble getting my unity gameobject to rotate in the manner I would like.

I would like my gameobject to act kind of like a turtle from a drawing program, where I could input a Vector3 like (10, 25, -5) and then it would rotate around its x axis by 10 degrees. Then rotate around it's Y axis by 25 degrees, and then rotate around it's z axis by -5 degrees.

Here's a picture example: (rotations not exact)

enter image description here

The problem is that the axes (the green, blue, and red gizmos you see when you have an object selected) change direction as the "turtle" goes through the Vector3.

It seems like transform.Rotate(new Vector3(10,25,-5), Space.Self) should be what I'm looking for. But it doesn't give me the result I would like! It still seems to rotate around it's start up, right, and forward vectors.

public Vector3 rotation;

Quaternion zero = new Quaternion(0,0,0,1)

void Update()
{
    transform.rotation = zero;
    transform.Rotate(rotation, Space.Self);
}

I have also tried doing some things with Quaternions to get the desired result, but I can't figure out exactly what is need. If anyone has any advise it would be greatly appreciated!

\$\endgroup\$
1
\$\begingroup\$

By nature, rotating an object will change its local axes. If you want a persistent set of "local axes", you can parent the gameobject to another gameobject, which holds the "local axes".

You can rotate around these axes in a custom order using Transform.RotateAround:

void Update()
{
   Transform par = transform.parent.transform;
   transform.rotation = Quaternion.Identity;
   transform.RotateAround(transform.position, par.right, rotation.x);
   transform.RotateAround(transform.position, par.up, rotation.y);
   transform.RotateAround(transform.position, par.forward, rotation.z);
}
\$\endgroup\$
0
\$\begingroup\$

Unity's default rotation order when specified in local Euler angles is:

  1. Rotate angles.z degrees around parent's Z+ axis
  2. Rotate angles.x degrees around parent X+
  3. Rotate angles.y degrees around parent Y+

Or, equivalently (if we start from the identity rotation):

  1. Rotate angles.y degrees around local Y+ axis
  2. Rotate angles.x degrees around the resulting local X+ axis
  3. Rotate angles.z degrees around the resulting local Z+ axis

Which differs from your desired order by exchanging x & y. To change this, we can define our own version of the Rotate method:

public static class RotationExtensions {

    static void RotateCustom(this Transform t, Vector3 angles) {
        // Do the x rotation first.
        t.Rotate(angles.x, 0, 0);
        // Then do the y & z rotations.
        angles.x = 0;
        t.Rotate(angles);
    }
    static void RotateCustom(this Transform t, float x, float y, float z) {
        t.RotateCustom(new Vector3(x, y, z));
    }
}

Now you can call transform.RotateCustom(angles) the way you would with regular Transform.Rotate to get your custom rotation order.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.