Lesson 96 +10 XP

Ragdoll Physics

Ragdoll Physics

A ragdoll is a physically simulated character created from a hierarchy of Rigidbodies and Joints. When activated, the character goes limp and realistically responds to forces and collisions.

Creating a Ragdoll

Unity has a built-in Ragdoll Wizard:

  1. Select the root bone of your character's hierarchy.
  2. Go to GameObject > 3D Object > Ragdoll....
  3. In the Ragdoll Wizard, assign your character's bones (Pelvis, Left Hips, Right Arm, etc.).
  4. Click Create. Unity adds Rigidbody, CharacterJoint, and Collider components to each bone.

Activating and Deactivating Ragdoll at Runtime

The standard pattern is to keep ragdoll physics disabled during normal gameplay and enable it on death:

using UnityEngine;

public class RagdollController : MonoBehaviour
{
    private Rigidbody[] _ragdollBodies;
    private Collider[] _ragdollColliders;
    private Animator _animator;

    void Awake()
    {
        _animator = GetComponent<Animator>();
        // Get all Rigidbodies in children (the bones)
        _ragdollBodies = GetComponentsInChildren<Rigidbody>();
        _ragdollColliders = GetComponentsInChildren<Collider>();
        // Start with ragdoll disabled
        SetRagdollActive(false);
    }

    void SetRagdollActive(bool active)
    {
        // Toggle physics on each bone
        foreach (Rigidbody rb in _ragdollBodies)
        {
            rb.isKinematic = !active;
        }
        foreach (Collider col in _ragdollColliders)
        {
            col.enabled = active;
        }
        // Toggle Animator (ragdoll and Animator don't mix)
        _animator.enabled = !active;
    }

    public void Die(Vector3 forceDirection)
    {
        SetRagdollActive(true);
        // Apply a death force to the pelvis
        Rigidbody pelvis = _ragdollBodies[0];
        pelvis.AddForce(forceDirection * 500f, ForceMode.Impulse);
    }
}

Constraints and Joint Limits

Each CharacterJoint added by the wizard has angular limits that prevent bones from bending unnaturally. You can adjust these in the Inspector to tune the ragdoll feel.

Key properties on CharacterJoint:

  • Low/High Twist Limit: Limits rotation along the joint's primary axis.
  • Swing 1/2 Limit: Limits lateral bending.

Performance Considerations

  • Ragdolls with many bones are expensive. For background characters, use simplified ragdolls (fewer bones).
  • Only activate ragdoll physics on characters that are actually visible and near the player.
  • Consider disabling ragdoll after a few seconds and hiding/destroying the object.

TL;DR

  • Use the Ragdoll Wizard (GameObject > 3D Object > Ragdoll) to auto-create joints and colliders.
  • In normal gameplay: isKinematic = true on all bones, Animator enabled.
  • On death: isKinematic = false on all bones, Animator disabled, apply an impulse force.
  • Tune CharacterJoint limits to prevent unnatural bone angles.