Strange leg collision after adding Ragdoll

After setting up the Ragdoll class and adding it to my characters, I noticed their legs were pushed further apart than they used to be. This happens to the player too, but I didn’t include a screenshot of that because it’s harder to see.

I set the Tags and Layers and the Physics settings exactly as they are shown in the video as well as added the Ragdoll Tag and Layer to the necessary objects.

When either the player or the enemy dies, they ragdoll appropriately.

When I examine either in the inspector, I can see that collision is turned off for the ragdoll parts.

Below is my Ragdoll class:

    private readonly string RagdollTagName = "Ragdoll";

    [SerializeField] private Animator animator;
    [SerializeField] private CharacterController controller;

    private Collider[] allColliders;
    private Rigidbody[] allRigidbodies;

    private void Start()
    {
        // This is an expensive operation. Try to use as sparingly as possible.
        allColliders = GetComponentsInChildren<Collider>(true);
        allRigidbodies = GetComponentsInChildren<Rigidbody>(true);

        // Turn everything off at the start.
        ToggleRagdoll(false);
    }

    public void ToggleRagdoll(bool isRagdoll)
    {
        foreach(Collider collider in allColliders)
        {
            if (collider.gameObject.CompareTag(RagdollTagName))
            {
                collider.enabled = isRagdoll;
            }
        }

        foreach (Rigidbody rigidbody in allRigidbodies)
        {
            if (rigidbody.gameObject.CompareTag(RagdollTagName))
            {
                // When isKinematic is turned off, the rigidbody will become affected by physics
                // If we want to ragdoll, we need to turn off isKinematic
                rigidbody.isKinematic = !isRagdoll;
                rigidbody.useGravity = isRagdoll;
            }
        }

        // If we want to ragdoll, we need to turn off the controller and animations
        controller.enabled = !isRagdoll;
        animator.enabled = !isRagdoll;
    }

I was able to fix this by manually flipping all the same flags that calling ToggleRagdoll(false) would do on just LeftUpLeg, LeftLeg, RightUpLeg, RightLeg. As a shot in the dark, it seems to be a race condition of some kind? Almost like the legs are colliding with each other before the code to turn off collision actually runs. I would like to be able to turn off collision programmatically. Any help would be appreciated!

I think you could use Awake instead of Start. Mine is in Awake and works fine, but it also works fine in Start.

You can also adjust the size of the colliders manually. It’s sometimes necessary to prevent the ragdoll from stretching itself into weird abominations.

Actually, it looks like the leg colliders are running up against the Capsule Collider. I’d start (I know you mentioned it already) but double checking to ensure that the colliders in question are indeed tagged with Ragdoll.

One other trick you can do is to ensure that the Capsule Collider and the Ragdoll colliders never actually interfere with one another. You can do this by putting the top level Player and Enemy on one layer, and all of the Ragdoll colliders on another layer. Then in the ProjectSettings/Physics, turn off the intersection between these two layers.

Well, it turns out the solution was the old fashioned “turn it off and then back on again.” After double checking everything, I gave up and closed Unity. I just opened it today for the first time since making the post and everything seems to be working exactly as it should. Not really sure where the problem resided in this case. Anyway, thanks the help!

Privacy & Terms