Game Development Reference
In-Depth Information
Figure 8-8. The new Ammo tag
5.
Select the PotatoAmmo prefab in the Project view, and tag it as Ammo.
6.
Create a new C# Script in the Game Scripts folder, and name it ReceivedHit .
Add the following OnCollisionEnter function:
7.
void OnCollisionEnter (Collision collision) {
if (collision.transform.tag == "Ammo") {
// if it was hit by something tagged as a Ammo, process its destruction
DestroyBun();
}
}
8.
Add the following function to destroy the object:
void DestroyBun () {
Destroy(gameObject, 0.2f); // destroy it after a brief pause
}
In case you are wondering why you didn't just destroy it directly from the OnCollisionEnter function,
it's because by separating it out, you can destroy it remotely. For example, you may decide that
the potato should destroy anything within a particular radius, similar to a grenade. The DestroyBun
function could be called from the projectile when it hits, once a sphere of influence is checked.
9.
Save the script, and add it to the ZombieBunny prefab in the Project view.
10.
Click Play, and shoot some critters.
If you are a pretty good shot, you may be able to get rid of all the originals before the next generation
comes in. That will eventually signal a level or the game to be finished.
Now that you are able to remove the pests from the scene, you will want to update the current
zombie bunny count, currentBunCount . If you remember, this variable is on the SpawnBunnies script
on the Zombie Spawn Manager object. The only problem is that, at any given time, the entire garden
may be deactivated. The answer, which will also be useful when you develop the GUI for the game,
is to keep track of the count on an independent object.
 
Search WWH ::




Custom Search