Game Development Reference
In-Depth Information
You are sending the message to destroy the hit objects directly from the Projectile script. Because
you already abstracted the destroy sequence out of the ReceivedHits script's OnCollisionEnter
function, this will be an easy task. Where before you called the DestroyBun function from the zombie
bunny that was hit, now you will have an array of zombie bunnies that are within range of the
projectile's contact point. You could call DestroyBun directly from the Projectile script, but by going
through a more generic function, you can both filter out for damage points and allow other objects
(such as cabbages) to be affected by the explosions also.
1.
Open the ReceivedHits script.
2.
Add the following variable:
int damage = 0; // accumulated damage points
3.
Add the following function:
void Terminator (int newDamage) {
damage += newDamage; // add more damage from
print (damage);
if (damage > 30) DestroyBun();
}
With the Projectile script telling the nearby zombie bunnies they've been hit, you can comment out
the OnCollisionEnter function in the ReceivedHits script.
Comment out the OnCollisionEnter function.
4.
5.
Save the script.
6.
Click Play, and try to hit the zombie bunnies.
This time they take a couple of hits before they have enough damage to be destroyed. Having to
shoot at each one twice is counterproductive. By altering the damage sent, you can set it to a 1-in-3
chance that the zombie bunny doesn't die and can receive the physics force.
7.
Change the contents of the Terminator code to the following:
if (damage > 10) DestroyBun (); // destroy only if there's enough damage
Comment out the print line.
8.
9.
Save the script.
Back in the Projectile script, change the SendMessage line to include a bit of
randomness:
10.
hit.gameObject.SendMessage ("Terminator", damage + Random.Range(0,2), SendMessageOptions.
DontRequireReceiver);
11.
Save the script, click Play, and try again.
 
Search WWH ::




Custom Search