Rotate around 3 axis

I’m working on a function that merges two gameobjet’s meshes into a single one, in order to get the right position of the vertices I need to rotate all the vertices of the second meshes around all 3 axis, to do so I’m using the following code:

Move.RotateAround(Vector3.zero,Vector3.right ,angle.x);
Move.RotateAround(Vector3.zero,Vector3.up ,angle.y);
Move.RotateAround(Vector3.zero,Vector3.forward,angle.z);

But it only works correctly when **angle.z** is equal to zero, or when **angle.z** is different than zero and **angle.y** is zero, in all other cases I get imprecise results. The reason for this is that when I rotate around the X axis, the coordinates Y and Z change, so if I then rotate around the Z axis (which adjusts the X and Y coordinates), the value of Y has already changed so I’m actually applying some extra degrees to the rotation which leads to imprecision.

On the other hand, while I was experimenting with lighting I noticed that some times when I changed the rotation of the light’s transform in the inspector, if I changed the Z rotation it automatically affected the value of the X rotation and other times a change in the X rotation affected the Z rotation, I didn’t understand why at that moment, but now it seems obvious; Unity has and internal algorithm that kicks in when two rotations affect the same coordinate, can anyone explain how it works or the math involved?

You’re using the global axes for all three rotations. Try using Move.up and Move.forward instead of Vector3.up and Vector3.forward in the second and third lines.

Your observation does make scene, and pointed me in the right direction, but only Move.forward is needed, and you also need to reset the rotation for a second run, like this:

Move.rotation = Quaternion.identity;
Move.RotateAround(Vector3.zero,Vector3.right,angle.x);
Move.RotateAround(Vector3.zero,Vector3.up ,angle.y);
Move.RotateAround(Vector3.zero,Move.forward ,angle.z);

Thanks a lot!!!

1 Like