How to do it...

To create a humanoid ragdoll, follow these steps:

  1. Place your character in the scene.
  2. Go to Game Object3d ObjectRagdoll to open the Create Ragdoll window.
  3. Drag and drop the bones from your character rig to the fields in the Create Ragdoll window. The fields' names aren't very accurate. Left Hips means left thigh, Left Knee means left shin, LeftArm means the left upper arm, and LeftElbow means the lower left arm, as shown in the following screenshot. The same applies to the right side. The rest of the names are self-explanatory:
Create Ragdoll window
  1. After you assign all the bones, click on the Create button. The ragdoll will be created. RigidbodyCapsule Collider (or Box Collider), and Character Joint components will be added to the required bones.
  2. You may need to adjust the size of the colliders to better match your character's shape. See the following screenshot:
Configured ragdoll
  1. Add two layers in the game: Characters and Ragdoll. To do so, go to EditProject SettingsTags and Layers.
  2. Set the character's layer to Characters and its rig with all its children (the ragdoll) to Ragdoll.
  3. Go to EditProject SettingsPhysics and disable the collision between the Characters and Ragdoll layers. This is a good way to prevent our ragdoll colliding with our character's main collider which would cause unwanted behavior (jitter, glitches in movement, and so on).
  4. Create a new C# script and name it HandleRagdoll.cs. In this script, we have the public void EnableRagdoll(bool enable, List<Rigidbody> rigidbodies) function. It iterates through all the rigid bodies attached to any of the children of the public Transform charactersRig transform and enables them or disables them depending on the enable flag. It also enables or disables the mainRigidbody and the mainCollider (attached to the character's root object). Finally, it enables or disables the character's Animator component:
      public void EnableRagdoll(bool enable, List<Rigidbody> 
      rigidbodies) 
      { 
          for (int i = 0; i < rigidbodies.Count; i++) 
          { 
              rigidbodies[i].isKinematic = !enable; 
          } 
          if (mainCollider != null) 
          { 
              mainCollider.enabled = !enable; 
          } 
          if (mainRigidbody != null) 
          { 
              mainRigidbody.isKinematic = enable; 
          } 
          if (anim != null) 
          { 
              anim.enabled = !enable; 
          } 
      } 
  1. We call this function every time player presses the space bar. At the start of the game, we collect all rigid bodies attached to character's rig children objects and store them in a global list. We pass that list to the EnableRagdoll() function every time we call it. We also call the EnableRagdoll() function at the start of the game with the enable parameter set to false. This disables the ragdoll.
  2. Attach the script to the character and drag its rig to the Characters Rig field of the script.
  3. Play the game and press the space bar to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.220