i like to rotate a cube on the vertical as well as the horizontal axis.
For this I have 4 inputs. Rotate left/right and up/down. The input ranges from -180 to 180 degrees on each axis.
I have tried this so far:
Vector3 targetEulerAngles = new Vector3(y, 0, 0); // Target rotation in Euler angles
m_transForYaw.eulerAngles = (targetEulerAngles);
targetEulerAngles = new Vector3(0, 0, -x);
m_tranfForPitch.eulerAngles= (targetEulerAngles);
m_transRotateAround.rotation = m_transForYaw.rotation * m_tranfForPitch.rotation;
My question/ problem is: when the x rotation is over 90 then the m_transForYaw does not rotate to the side but rotates around the forward vector.
I have also tried Rotate around.
Do you know how to solve this that it only rotates to left/right regardless of prevous rotations.
As I see it the problem is that I previously rotated the cube and thats the problem. E.g.
- I rotated on Vector.right -> normal behaviour -> the cube turns forward/backwards
- Now I rotate on Vector.up. -> the cube does now not rotate to the left/right but rotates around the global Vector.forward axis
8
Interpreting the question as a “why can this be expressed as a series of rotations in local space” question
For the case of an object starting as non-rotated, combining rotations in a certain order in the axes as they change each time (in other words, local axes) is always equivalent to combining rotations in the reverse order on the global axes. This is just a quirk how rotations work in euclidean/3d space and not something that can be avoided.
Note that the bottom path shows the result of only global rotations (which as far as I can tell is what you are after) and the top path is how you described it in your diagram and text which is explaining that it is incorrect. Both are valid interpretations of outputTransform.rotation = Quaternion.Euler(-90f,0f,0f) * Quaternion.Euler(0f,-90f,0f);
.
And this equivalence is true of any-sized sequence of rotations (again, starting with a non-rotated object)
2