Search Unity

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

Discussion in 'Physics' started by niuage, Dec 7, 2023.

  1. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    123
    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:


    Code (CSharp):
    1. private void CrumbleOnDeath()
    2. {
    3.     if (!crumbleOnDeath) return;
    4.  
    5.     foreach (GameObject bodyPart in bodyParts) {
    6.  
    7.         BoxCollider collider = bodyPart.AddComponent<BoxCollider>();
    8.         Rigidbody rb = bodyPart.AddComponent<Rigidbody>();
    9.         rb.useGravity = true;
    10.         rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
    11.         rb.mass = 1;
    12.         rb.drag = 5;
    13.     }
    14. }
    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.
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    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.
     
  3. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    123
    Thanks :)

    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.
     
  4. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    123
    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?

     
  5. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    123
    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.
     
  6. bureviy

    bureviy

    Joined:
    May 4, 2022
    Posts:
    2
    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.