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 for our character whenever he steps on a landmine object. We will also use this opportunity to reset the character's position and animations a number of seconds after that event.

Getting ready

For this recipe, we have prepared a project named MixamoProject containing several assets such as levels, animated characters, and props. You can find it inside the 0423_05_codes folder.

How to do it...

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

  1. Open the MixamoProject project, and then open the level named 05_07 (in the Levels folder).
  2. You should see Mixamo's animated S.W.A.T. soldier and two cylinders (the landmine and the spawn point).
  3. First, let's set up our ragdoll. Go to GameObject | Create Other | Ragdoll.... The Ragdoll Wizard should pop up.
  4. Assign the transforms as follows (these are also shown in the following screenshot):
    • Root: swat:Hips
    • Left Hips: swat:LeftUpLeg
    • Left Knee: swat:LeftLeg
    • Left Foot: swat:LeftFoot
    • Right Hips: swat:RightUpLeg
    • Right Knee: swat:RightLeg
    • Right Foot: swat:RightFoot
    • Left Arm: swat:LeftArm
    • Left Elbow: swat:LeftForeArm
    • Right Arm: swat:RightArm
    • Right Elbow: swat:RightForeArm
    • Middle Spine: swat:Spine1
    • Head: swat:Head
    How to do it...
  5. In 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 {
        private float hitTime;
        private bool wasHit = false;
    
        void Start () {
            DeactivateRagdoll();
        }
        void Update () {
            if(wasHit){
                if(Time.time >= hitTime + 5.0f)
                    DeactivateRagdoll();
                }
            }
        public void ActivateRagdoll(){
            this.GetComponent<BasicController>().enabled = false;
            this.GetComponent<Animator>().enabled = false;
            foreach(Rigidbody bone in GetComponentsInChildren<Rigidbody>()){
                bone.isKinematic = false;
                bone.detectCollisions = true;
            }
            wasHit = true;
            hitTime = Time.time;
        }
        public void DeactivateRagdoll(){
            this.GetComponent<BasicController>().enabled = true;
            this.GetComponent<Animator>().enabled = true;
    
            foreach(Rigidbody bone in GetComponentsInChildren<Rigidbody>()){
                bone.isKinematic = true;
                bone.detectCollisions = false;
            }
            transform.position = GameObject.Find("Spawnpoint").transform.position;
            transform.rotation = GameObject.Find("Spawnpoint").transform.rotation;
            wasHit = false;
        }
    }
  7. Save and close the script.
  8. Attach the RagdollCharacter.cs script to the Character game object.
  9. In 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 = 50.0f;
        public float force = 2000.0f;
    
        void OnTriggerEnter ( Collider collision ){
            if(collision.gameObject.tag == "Player"){
    collision.GetComponent<RagdollCharacter>().ActivateRagdoll();
                Vector3 explosionPos = transform.position;
                Collider[] colliders = Physics.OverlapSphere(explosionPos, range);
                foreach (Collider hit in colliders) {
                    if (hit.rigidbody)
                        hit.rigidbody.AddExplosionForce(force, explosionPos, range, 3.0F);
                }
            }
        }
    }
  11. Save and close the script.
  12. Attach the script to the Landmine game object.
  13. Play the scene. Use the WASD keyboard control scheme to direct the character to the Landmine game object. Colliding with it will activate the character's ragdoll physics and apply an explosive force to it. As a result, the character will be thrown to a considerable distance.

How it works...

Unity's Ragdoll Wizard assigns the Collider, Rigidbody, and Character Joint components to selected transforms. When used in conjunction with each other, these components make ragdoll physics possible. However, they must be disabled whenever we want our character to be animated or 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 testing purposes, we have also created the Landmine script, which calls RagdollCharacter's function, ActivateRagdoll(). It also applies an explosive force to our ragdoll character, violently throwing him outside the explosion site.

There's more...

Instead of resetting the character's transform settings, you could have destroyed his game object and instantiated a new one over the re-spawn point using tags. For more information on that subject, check Unity's documentation at the following location: http://docs.unity3d.com/Documentation/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
18.226.251.206