- Home /
Rotate rigidbody using MoveRotation() around the global y axis
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()?
Ok not to worry I have solved it with a workaround. I just made an empty game object called direction of which i use the old transform.rotation code i originally used and in fixedupdate i just use the following line :
rigidbody.$$anonymous$$oveRotation (Quaternion.LookRotation (direction.forward));
Answer by lgarczyn · Jan 18, 2020 at 06:55 PM
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
Answer by robertbu · Nov 10, 2014 at 03:17 PM
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);
Your answer
Follow this Question
Related Questions
Constrain Turret Rotation while child of Rigidbody 2 Answers
use world axis when using transform.rotate 3 Answers
use world axis when using transform.rotate 0 Answers
Rotating Player Controller locally or globally. 0 Answers
how to tilt boat on turning 2 Answers