In this article we will learn how to apply Ragdoll physics to a character.
(For more resources related to this topic, see here.)
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.
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.
To apply ragdoll physics to your character, follow these steps:
Insert image 1362OT_07_45.png
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();
}
}
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;
}
}
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 including 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.
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.
In this article we learned how to apply Ragdoll physics to a character. We also learned how to setup Ragdoll for the character of the game.
To learn more please refer to the following books:
Further resources on this subject: