Applying Ragdoll physics to a character

Action games often make use of Ragdoll physics to simulate the character's body reaction to being unconsciously under the effect of a hit or explosion. In this recipe, we will learn how to set up and activate Ragdoll physics to our character whenever she steps in a landmine object. We will also use the opportunity to reset the character's position and animations a number of seconds after that event has occurred.

Getting ready

For this recipe, we have prepared a Unity Package named Ragdoll, containing a basic scene that features an animated character and two prefabs, already placed into the scene, named Landmine and Spawnpoint. The package can be found inside the 1362_07_08 folder.

How to do it...

To apply Ragdoll physics to your character, follow these steps:

  1. Create a new project and import the Ragdoll Unity Package. Then, from the Project view, open the mecanimPlayground level.
  2. You will see the animated MsLaser character and two discs: Landmine and Spawnpoint.
  3. First, let's set up our Ragdoll. Access the GameObject | 3D Object | Ragdoll... menu and the Ragdoll wizard will pop-up.
  4. Assign the transforms as follows:
    • Pelvis: mixamorig:Hips
    • Left Hips: mixamorig:LeftUpLeg
    • Left Knee: mixamorig:LeftLeg
    • Left Foot: mixamorig:LeftFoot
    • Right Hips: mixamorig:RightUpLeg
    • Right Knee: mixamorig:RightLeg
    • Right Foot: mixamorig:RightFoot
    • Left Arm: mixamorig:LeftArm
    • Left Elbow: mixamorig:LeftForeArm
    • Right Arm: mixamorig:RightArm
    • Right Elbow: mixamorig:RightForeArm
    • Middle Spine: mixamorig:Spine1
    • Head: mixamorig:Head
    • Total Mass: 20
    • Strength: 50
    How to do it...
  5. From the Project view, create a new C# Script named RagdollCharacter.cs.
  6. Open the script and add the following code:
    using UnityEngine;
    using System.Collections;
    
    public class RagdollCharacter : MonoBehaviour {
    
      void Start () {
          DeactivateRagdoll();
        }
    
        public void ActivateRagdoll(){
        gameObject.GetComponent<CharacterController> ().enabled = false;
        gameObject.GetComponent<BasicController> ().enabled = false;
        gameObject.GetComponent<Animator> ().enabled = false;
        foreach (Rigidbody bone in GetComponentsInChildren<Rigidbody>()) {
            bone.isKinematic = false;
            bone.detectCollisions = true;
        }
        foreach (Collider col in GetComponentsInChildren<Collider>()) {
            col.enabled = true;
        }
        StartCoroutine (Restore ());
    
        }
      public void DeactivateRagdoll(){
    
        gameObject.GetComponent<BasicController>().enabled = true;
        gameObject.GetComponent<Animator>().enabled = true;
        transform.position = GameObject.Find("Spawnpoint").transform.position;
        transform.rotation = GameObject.Find("Spawnpoint").transform.rotation;
        foreach(Rigidbody bone in GetComponentsInChildren<Rigidbody>()){
            bone.isKinematic = true;
              bone.detectCollisions = false;
          }
        foreach (CharacterJoint joint in GetComponentsInChildren<CharacterJoint>()) {
          joint.enableProjection = true;
        }
        foreach(Collider col in GetComponentsInChildren<Collider>()){
          col.enabled = false;
        }
      gameObject.GetComponent<CharacterController>().enabled= true;
    
        }
    
      IEnumerator Restore(){
        yield return new WaitForSeconds(5);
        DeactivateRagdoll();
      }
    }
  7. Save and close the script.
  8. Attach the RagdollCharacter.cs script to the MsLaser GameObject. Then, select the MsLaser character and, from the top of the Inspector view, change its tag to Player.
  9. From the Project view, create a new C# Script named Landmine.cs.
  10. Open the script and add the following code:
    using UnityEngine;
    using System.Collections;
    
    public class Landmine : MonoBehaviour {
      public float range = 2f;
      public float force = 2f;
      public float up = 4f;
      private bool active = true;
    
      void  OnTriggerEnter ( Collider collision  ){
        if(collision.gameObject.tag == "Player" && active){
          active = false;
          StartCoroutine(Reactivate());
          collision.gameObject.GetComponent<RagdollCharacter>().ActivateRagdoll();
          Vector3 explosionPos = transform.position;
                Collider[] colliders = Physics.OverlapSphere(explosionPos, range);
              foreach (Collider hit in colliders) {
            if (hit.GetComponent<Rigidbody>())
                      hit.GetComponent<Rigidbody>().AddExplosionForce(force, explosionPos, range, up);
                 }
            }
        }
      IEnumerator Reactivate(){
        yield return new WaitForSeconds(2);
        active = true;
      }
    }
  11. Save and close the script.
  12. Attach the script to the Landmine GameObject.
  13. Play the scene. Using the WASD keyboard control scheme, direct the character to the Landmine GameObject. Colliding with it will activate the character's Ragdoll physics and apply an explosion force to it. As a result, the character will be thrown away to a considerable distance and will no longer be in the control of its body movements, akin to a ragdoll.

How it works...

Unity's Ragdoll Wizard assigns, to selected transforms, the components Collider, Rigidbody, and Character Joint. In conjunction, those components make Ragdoll physics possible. However, those components must be disabled whenever we want our character to be animated and controlled by the player. In our case, we switch those components on and off using the RagdollCharacter script and its two functions: ActivateRagdoll() and DeactivateRagdoll(), the latter includes instructions to re-spawn our character in the appropriate place.

For the testing purposes, we have also created the Landmine script, which calls RagdollCharacter script's function named ActivateRagdoll(). It also applies an explosion force to our ragdoll character, throwing it outside the explosion site.

There's more...

Instead of resetting the character's transform settings, you could have destroyed its GameObject and instantiated a new one over the respawn point using Tags. For more information on this subject, check Unity's documentation at http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html.

..................Content has been hidden....................

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