Game Development Reference
In-Depth Information
}
function DelayedDetonate () {
BroadcastMessage (“Detonate”);
}
function Detonate () {
Destroy(gameObject);
if (explosion)
Instantiate (explosion, transform.position, transform.rotation);
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, r
transform.position, transform.rotation);
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
emitter.emit = false;
emitter.transform.parent = null;
}
}
3. Once the script is completed, add it to the zombie's head mesh where you previously
placed the sphere collider. Change the Hit Points variable to
10
in the Inspector view.
The preceding script manages a
hitPoints
variable. When this variable's value reaches
0, the script makes the game object disappear and runs functions that replace the object
with a particle emitter and a dead replacement object. These can both be added in the
Inspector view. This script is a general damage receiving script that can be used and reused
for many types of objects.
You will not need a dead replacement for this particular instance, but a particle emitter
might create a good headshot effect. The online resources contain a prefab emitter called
HeadAsplode
that can show you how to create a blood effect, as shown in Figure 10.11.
1. Select the
ProfessorZombie
object and add a new JavaScript called
BodyDestroy.js
to
your
Scripts
folder. This script is based on the preceding damage receiving script but
adds a condition that allows it to work with the head object. The script looks for the
existence of the
ProfZombieHead
object within the child objects of
ProfessorZombie
and
destroys itself if
ProfZombieHead
no longer exists. The script is as follows:
var deadReplacement : Transform;
var DeadZombie : boolean = false;
var PointValue : float = 100;
function Update () {
transform.Find(“ProfZombieHead”);
if (transform.Find(“ProfZombieHead”) == false) {
DeadZombie = true;
Search WWH ::
Custom Search