- Home /
How do i keep a rigidbody upright? Looking for best way to freeze rotation
I am working on a making a ridable horse and trying to figure out how to keep the rigidbody upright by not rotating the z axis. I have gone through so many posts and videos looking for an answer but seems like so many different solutions, many of which suggest using transform.rotation and setting euler angles to 0 or something like that, and I am looking for one that's more efficient and reliable, avoiding gimbal lock and just rotating the rigidbody directly. This kind of got it working in FixedUpdate and keeping it from falling over sideways, but the movement becomes choppy and bouncing around a bit. I feel like this must be a very common issue but doesn't seem to be a common solution. Thanks for any help!
Quaternion rotationUpright = Quaternion.LookRotation(rbMount.transform.forward, Vector3.up);
rbMount.rotation = rotationUpright;
Answer by Namey5 · Apr 04, 2020 at 10:26 PM
As noted by the docs;
Note that [Rigidbody] position constraints are applied in World space, and rotation constraints are applied in Local space.
https://docs.unity3d.com/ScriptReference/Rigidbody-constraints.html
This means that if you want to freeze rotation around the local z-axis, you can just constrain that axes' rotation (either through script, or through the editor). Aside from that, you could do the above but instead of directly setting the rotation use the MoveRotation method;
Quaternion rotationUpright = Quaternion.LookRotation(rbMount.transform.forward, Vector3.up);
GetComponent<Rigidbody>().MoveRotation (rotationUpright);
This will do the same thing, but the rotation will be run through the physics simulation, so it should be much smoother.
Hey thanks a lot! That seems to have done it and is working a lot smoother now.
Your answer
Follow this Question
Related Questions
Physics discrepency 1 Answer
Creating draggable hinge joint 1 Answer
Player Rotation with the mouse 1 Answer
Rigidbody rotation with PID controller and torque 0 Answers
Prevent Rigidbody From Rotating 3 Answers