Dynamically adding rigidbody and colliders to parts of a character makes it fly off

Here’s a video of the issue:

When this imp character dies, I want its body parts to fall on the ground. So my idea was to iterate over some body parts, and add rigidbodies and colliders.

All the game objects are on the same layer, “Mob”, and in the Physics collision matrix, Mob cannot collide with Mob.

Any idea what could cause the parts to fly off into the distance whenever I add physics to them? Even if I put a high drag, then they still fly off insanely quick.

Basic code:

private void CrumbleOnDeath()
{
    if (!crumbleOnDeath) return;

    foreach (GameObject bodyPart in bodyParts) {

        BoxCollider collider = bodyPart.AddComponent<BoxCollider>();
        Rigidbody rb = bodyPart.AddComponent<Rigidbody>();
        rb.useGravity = true;
        rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
        rb.mass = 1;
        rb.drag = 5;
    }
}

I played with the mass and drag values but I dont think they’re the root cause. Even with heavy mass they fly off super quick.

It feels like it happens when the body parts collide with the floor, but if I go to the scene view, and grab 1 body part and drag it over the terrain and drop it, then it works fine, it just collide properly.

Thanks.

It’s caused by your objects overlapping when you turn them into rigidbodies. Before turning them into a rigidbody you can try moving them out from the character’s center by 0.3 meters or so. Or create an array with the necessary clear positions for the body parts.

Or just add even more drag (including angular drag) to slow them down. Although a very high drag setting will cause them to seemingly float down to the ground.

Thanks :slight_smile:

But is the overlapping a problem if all the objects are on the same Mob layer, and I set the Mob layer not to collide with itself.

Also, looking at this still frame from the video, it looks like the different part didnt push each other off, so might not be the overlap that’s an issue?

I think it has to do with the character controller… I tried to delete the component, then wait 1 frame, but it was still not working.

But when I move the gameobject containing the character model OUT of the main gameobject before adding the colliders etc, then it works perfectly.

There was a similar problem. The Animator on the gameObject played the animation of the character’s parts, and when these parts became dynamic, everything broke. If the object has an Animator component, it is better to disable it.