Game Development Reference
In-Depth Information
There is an exception to this rule; the scripts in the Standard Assets folder are compiled even before
the JavaScript files. The quick solution here is in the Project panel: move the Sample Assets ➤
Characters ➤ Third Person Character folder by dragging and dropping it into the Standard Assets
folder, and also dragging and dropping the Sample Assets ➤ Cross Platform Input folder into the
Standard Assets folder. Next, in the Project panel search bar find the CrossPlatformInputInitialize
script and delete it. Recall that at the time of this writing, Sample Assets (beta) is a separate folder,
but eventually these assets will be integrated into Standard Assets. If this is the case for you, then
you can skip this step.
In the Project panel, open your Assets ➤ Prefabs folder. Drag the Third Person Character Ragdoll
from the Hierarchy to the Assets ➤ Prefabs folder to save it as a prefab.
Now to write a script for activating the ragdoll physics when the player character is impacted by a
pendulum.
In the Project panel's Assets ➤ Scripts folder, select Create ➤ JavaScript, name it KillOnCollision,
and open it in MonoDevelop. Edit the code to the following:
#pragma strict
function OnCollisionEnter(other : Collision) {
if (other.gameObject.name == "Third Person Character Ragdoll")
{
other.gameObject.GetComponent(GoRagdoll).GotoRagdoll();
}
}
This is all familiar from Chapter 6. The code breaks down as follows:
(1) function OnCollisionEnter(other : Collision)
When the Collider component of the game object to which this script is attached detects a collision,
the OnCollisionEnter function is called, and passes in a reference other to the game object it has
collided with (the player character).
(2) if (other.gameObject.name == "Third Person Character Ragdoll")
Check to see if it was the Third Person Character Ragdoll game object that was collided with.
(3) other.gameObject.GetComponent(GoRagdoll).GotoRagdoll();
Use the GetComponent function to access the GoRagdoll script and then call the GotoRagdoll()
function.
In this particular case, the conditional is necessary because sometimes when the player character
gets taken out by a pendulum, the pendulum will continue to collide with one or more of the ragdoll
colliders, which would cause an error. The same thing would happen if you had moving objects other
than the player character that ran into the pendulum.
Save the script and attach it to each of the three Pendulum Spheres. Save the scene, save the
project, and playtest. Now THAT is a satisfying response to getting smacked by a pendulum!
(See Figure 7-14 .)
 
Search WWH ::




Custom Search