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!