- Home /
Rigidbody keep balance
I've been trying to figure out a way to balance a rigidbody at an certain angle. What i have is a rigidbody standing up, and i want it to balance at an angle "theta" which i can control: So i want to make theta equal to 0, like this:
It's important that it's done by the physics engine, but i can't figure out how to do. I've tried messing around with the center of mass but never succeded. Any ideas? Thanks :)
Answer by ZannaU · Feb 02, 2017 at 10:36 PM
From your explanation it's not very clear what is your objective so here I give you the simplest solution. I 'm not sure it's what you are looking for.
If you just want a rigid body to keep a fixed angle you can freeze the rotation axes. It's a common practice for example to keep a character collision capsule up right.
Answer by Bunny83 · Feb 02, 2017 at 05:14 PM
Uhm, what do you mean by "balance"? An object can't stay at an arbitrary angle because it would not be balanced. So if you want to actively apply forces (or torque) to it to keep it's current orientation that have nothing to do with balance but would require a control system and you have to permantently applying a force / torque to keep it in it's position.
Such control systems can get very complex and have their limits (in regard of response time and error value). For things like that you usually use a PID controller. There are a few reference implementations of PID controllers in Unity
ps: just setting the center of mass is pointless when you only have a single collision point (or line in 3d) as it will never be stable in this position, even when the center of mass is exactly above the collision point.
Agree. I would suggest a spring joint connected between the top/center of the rectangle, to a fixed point directly above the collision point. By adjusting the force of the spring, you will be able to adjust angle theta. It may bounce a bit a first depending on initial conditions, but should settle down to equilibrium.