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.
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.
To apply ragdoll physics to your character, follow these steps:
RagdollCharacter.cs
.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; } }
Landmine.cs
.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); } } } }
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.
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
3.145.50.222