hasseyg
November 10, 2014, 3:12pm
1
Hi, I originally had this code to rotate my game object which worked fine:
transform.Rotate (new Vector3 (0, joytickX, 0), Space.World);
transform.Rotate (new Vector3 (joystickY, 0, 0), Space.Self);
But then I realised I needed to rotate the rigidbody instead so I changed it to this:
Quaternion deltaRotation = Quaternion.Euler (new Vector3 (joystickY, joytickX, 0));
rigidbody.MoveRotation (transform.rotation * deltaRotation);
The problem is that MoveRotation() rotates the game object using it’s local space, of which is fine for the x axis but for the y axis I want it to rotate using the global space instead. How can I achieve this whilst still using MoveRotation()?
Old post, but it shows up on Google, so:
the problem was the order of the multiplication. The left-most operand is the one on the outside, and the right-most operand is on the inside.
The only reason your rotation was “local” is because you used the wrong multiplication order in MoveRotation.
rigidbody.rotation * deltaRotation
will add deltaRotation to the current rotation. If your local Y axis is on its side, it will rotate around it.
deltaRotation * rigidbody.rotation
will rotate around the world Y axis instead, then restore your current rotation
You could use Quaternion.AngleAxis():
Quaternion deltaRotation = Quaternion.AngleAxis(joystickX, transform.up) * Quaternion.AngleAxis(joystickY, Vector3.right);
Without testing, I don’t know the exact recipe. You may have to reverse the order the two rotation are combined and/or negate one the angle.
Also I’d reverse the order in the MoveRotation():
rigidbody.MoveRotation (deltaRotation * transform.rotation);