- Home /
Combine multiple rigidbodies?
Hello there. I was wondering, what is the best way to approach this problem? I want to combine two or more different rigidbodies into one at runtime, or if that is not possible without a complex use of the Mesh class, to make them act as one? Picture shows what I mean, since I am not good with words:
They would need to follow the physics as if they were a single object, mass caclulations included. Thank you in advance.
~Bumbaz
Answer by Eric5h5 · Apr 27, 2011 at 09:15 PM
If you remove the rigidbodies from the cubes (but keep the colliders intact), and make them children of an empty gameobject that has a rigidbody, then they will be effectively merged. You'd want to add the mass from the two cubes before deleting the rigidbodies and apply that to the mass of the parent.
That's very helpful, thanks. But what if I have gameobjects with different masses? In the method you described, the center of mass is only one. Is it possible to assign different weights to different parts of this one gameobject?
I am so bad at words. Haha
~Bumbaz
@Bumbaz: No, that wouldn't work with the method I described. I'm not sure there's an easy way to do that.
what if you only parented them to the new gameobject but retained their original rigidbodies? Wouldn't that also retain their weight distribution?
@ChrisD: that won't work, since the rigidbodies would still be independent. In order for compound colliders to work, only the parent can have a rigidbody.
Answer by klay · Sep 07, 2012 at 08:32 PM
Maybe you need to look at a fixed joint:
http://docs.unity3d.com/Documentation/Components/class-FixedJoint.html
This is used to "connect" two distinct rigidbodies together. Each maintains its own mass, but pushing on one rigidbody pulls on the connected one, so they act sort of like a single physics object.
There are other types of joints as well: hinge joints, spring joints, etc.