Game Development Reference
In-Depth Information
Adding the Zombie Bunnies
The Gnomatic Garden Defender's main task will be to obliterate the ravening hoards of zombie bunnies
from your garden. To make things interesting, you will be dropping the varmints randomly around
the garden in random numbers and adding to the population at random times. In case you haven't
guessed yet, the key word here is “random.” Randomness is a staple of most games, but especially
casual games that are meant to be played over and over as the player refines his skill and cunning.
In this game, you will drop the zombie bunnies slightly off the ground and let them fall into place.
Occasionally, they may end up on head or tail, but the absurdity actually adds to the game, so you
will let them land at will. Let's begin by instantiating , or creating, one of the critters in the garden area
during runtime. The trick to instantiation is that you are not creating everything from scratch, you are
re-creating existing prefabs.
1.
Create a new C# Script, and name it SpawnBunnies .
2.
Add the following variable under the class declaration:
public GameObject zombieBunny; // the zombieBunny prefab
In the Start function, instantiate the prefab:
3.
Instantiate (zombieBunny); // create a new zombie bunny prefab in the scene
4.
Save the script.
Before you can test the script, you will have to put it on an object in the scene. You will be defining
an area where the zombie bunnies can be instantiated, but you will want to keep the script
abstracted from any particular garden in case you eventually have multiple gardens or levels.
5.
Create a new Empty GameObject, and name it Zombie Spawn Manager .
6.
Add the SpawnBunnies script to it.
7.
Drag the ZombieBunny prefab from the Prefabs' Characters folder in the
Project view to the Zombie Spawn Manager's Spawn Bunnies component's
Zombie Bunny parameter.
8.
Move the Gnomatic Garden Defender into the garden area, and disable the
Hide At Start component on the Garden 1 and Common Wall 3.
9.
Click Play, and look around the garden to find the new instantiated zombie
bunny.
The zombie bunny is instantiated in the scene at the last position he was in when you created or
updated his prefab (Figure 7-28 ).
 
Search WWH ::




Custom Search