In this section, we will apply the ragdoll to the AI ragdoll game object and replace it with the current AI game object when it dies.
Go to the Project view and open the Prefabs
folder inside the Chapter7
folder. Drag the robot_AI_ragdoll
prefab to the Hierarchy view. Then, go to the Scene view and press the F key to zoom into the robot_AI_ragdoll
game object in the scene.
Now, we can start applying ragdoll physics to the robot_AI_ragdoll
:
robot_AI_ragdoll
to see the child names roothandle
, then drag it to the Root in the Create Ragdoll window, as shown in the following screenshot:upleg_L
to the Left Hips in the Create Ragdoll window and click on the upleg_L to bring its child names lowleg_L.lowleg_L
to the Left Knee in the Create Ragdoll window. Then, we will click on the lowleg_L to bring up its child names heel_L and drag it to the Left Foot in the Create Ragdoll window.upleg_R
to the Right Hips, lowleg_R
to the Right Knee, and heel_R
and drag it to the Right Foot in the Create Ragdoll window, as shown in the following screenshot:We can also see the results as shown in the following screenshot:
robot_AI_ragdoll
, you will see that the capsule and box colliders add on each joint, as shown in the following screenshot:However, we are not done yet. From the preceding screenshot, we will see that we still need to adjust the colliders associated with the character shape.
head
in the Hierarchy view, go to the Inspector view, and set the following:roothandle
in the Hierarchy view, go to the Inspector view, and set the following:spine1
in the Hierarchy, go to the Inspector view, and set the following:robot_AI_ragdoll
game object.robot_AI_ragdoll
prefab in the Project view, we don't need the robot_AI_ragdoll
game object in the Hierarchy view anymore, so we just delete it by right-clicking on it and choosing Delete.AIController
script to enable the robot_AI_ragdoll
game object when the AI is dead. Let's open the AIController
script and add this script at the beginning, as highlighted in the following code:@script RequireComponent(CharacterController) //Ragdoll public var aiRagdoll : GameObject; //Waypoint public var wayPoint : Waypoints;
OnCollisionEnter(collision : Collision)
and add the following highlighted lines of code://Checking for the collision if the rocket hit the AI
public function OnCollisionEnter(collision : Collision) : void {
if (collision.transform.tag == "Rocket") {
var rocket : Rocket = collision.gameObject.GetComponent(Rocket);
var f_damage : float = rocket.getDamage();
aiHP -= f_damage;
b_isGotHit = true;
if (aiHP <= 0) {
aiHP = 0;
var obj_aiPrefab : GameObject = Instantiate(aiRagdoll, transform.position, transform.rotation);
GameObject.Destroy(transform.parent.gameObject);
}
}
}
AIController
script, and go back to the Project view; click on the AI Prefab, and go to the Inspector view.robot_AI_ragdoll
to this Ai Ragdoll parameter, as shown in the following screenshot:With that we are done with this step. We can click Play and see the result—when we kill the AI, we will see that the ragdoll game object ( the one that we just created) replaces the AI.
We just created the ragdoll prefab game object to replace the AI when the AI is dead, which will make it look realistic. We also have used GameObject.Destroy()
to destroy the AI game object in the scene and use the Instantiate()
function to clone the ragdoll prefab to replace the AI game object that has already been destroyed.
In this section, we created the ragdoll object to replace our Enemy
when it dies, which looks good. However, we will see that robot_AI_ragdoll
just fell down to the ground without any force from the rocket that was fired at it.
To make it much more fun and realistic, we can do this using two different equations.
First, we can add force (using the AddForce()
function) to our ragdoll's rigidbody
, which will make our ragdoll move following the rocket direction. We can do this by adding a script in the OnCollisionEnter(collision : Collision)
in the AIController
script, as highlighted in the following code:
//Checking for the collision if the rocket hit the AI public function OnCollisionEnter(collision : Collision) : void { if (collision.transform.tag == "Rocket") { var rocket : Rocket = collision.gameObject.GetComponent(Rocket); var f_damage : float = rocket.getDamage(); aiHP -= f_damage; b_isGotHit = true; if (aiHP <= 0) { aiHP = 0; var obj_aiPrefab : GameObject = Instantiate(aiRagdoll, transform.position, transform.rotation); var obj_aiPrefab : GameObject = Instantiate(aiRagdoll, transform.position, transform.rotation); /* Make the ragdoll react to the rocket force*/ var f_force : float = 1000; //Get transform direction of the rocket var v3_rocketDir : Vector3 = rocket.transform.TransformDirection(Vector3.forward); //Get the rigid body of gun and the ragdoll var a_rigid : Rigidbody[] = obj_aiPrefab.GetComponentsInChildren.<Rigidbody>(); //Apply force to the gun rigidbody and ragdoll for (var r : Rigidbody in a_rigid) { r.AddForce(v3_rocketDir * f_force); } GameObject.Destroy(transform.parent.gameObject); } } }
The second is using the explosive force (using the AddExplosionForce()
function), which will make our ragdoll move in a different direction depending on the distance from the explosive position to our ragdoll's rigidbody
object. We can do this by replacing the following script in the same function as in the preceding code:
//Checking for the collision if the rocket hit the AI public function OnCollisionEnter(collision : Collision) : void { if (collision.transform.tag == "Rocket") { var rocket : Rocket = collision.gameObject.GetComponent(Rocket); var f_damage : float = rocket.getDamage(); aiHP -= f_damage; b_isGotHit = true; if (aiHP <= 0) { aiHP = 0; var obj_aiPrefab : GameObject = Instantiate(aiRagdoll, transform.position, transform.rotation); /* Make the ragdoll react to the explosion force*/ var f_force : float = 1000; //Get the rigid body of gun and the ragdoll var a_rigid : Rigidbody[] = obj_aiPrefab.GetComponentsInChildren.<Rigidbody>(); //Apply force to the gun rigidbody and ragdoll for (var r : Rigidbody in a_rigid) { r.AddExplosionForce(f_force, rocket .transform.position, 100.0); } GameObject.Destroy(transform.parent.gameObject); } } }
The AddExplosionForce()
function basically applies a force to the rigidbody
, which will simulate the explosion effects. You can see more details on the AddExplosionForce()
function from the following Unity scripting document:
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddExplosionForce.html.
18.118.12.186