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.
Here is a video for showcasing the problem.
https://drive.google.com/file/d/1PZ4ItbmmSByU3dfGp2bmKzmcD1yqXBf_/view?usp=sharing
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)
Edit: In your particular case, you're comparing the results of two different sequences of rotations which start from the same orientation but one result being presented first which creates the illusion of the object being rotated from that first presented orientation. Note that your code doesn't use any data about the orientation from the previous frame at all.
Here's a diagram to help explain what that means in your case (I designated some local axes for your cube for the sake of illustration):
Suppose you have some orientation on frame A made up of two 90º rotations on x and y axes. Note that the result of frame A can be expressed as a sequence of local or global rotations (1st and 2nd rows) but the result is the same.
Suppose on frame B you then "add" another 90 degrees of rotation on the y axis. Because your eyes are seeing frame A before frame B, it appears like it must be a local rotation [see "BEFORE CALCULATIONS FRAME B (apparent)]. But you have to remember that your code is starting with the cube in the same unrotated state. That means you can also express that result as a series of global rotations (bottom row).
If you want a different kind of calculation, you probably want to use the orientation of the cube from the previous frame but once that is added as an input then the same knob positions will often not result in the same output orientation.
If you already have the x,y, and rotation from the previous frame, here's a method which calculates the rotation of the current frame. This is just one way of doing it:
private Quaternion HandleKnobs(float previousX, float previousY, float x, float y,
Quaternion previousRotation) {
float xDiff = x - previousX;
float yDiff = y - previousY;
Vector3 axis = new Vector3(y,-x,0f);
Vector3 magnitude = Mathf.Sqrt(xDiff*xDiff + yDiff*yDiff);
float scale = 2.0f // tune as appropriate
// do not include deltaTime here
return Quaternion.AngleAxis(magnitude * scale, axis) * previousRotation;
}
// Example use:
// m_transRotateAround.rotation = HandleKnobs(prevX, prevY, x, y,
// m_transRotateAround.rotation);