Game Development Reference
In-Depth Information
At the top of the PopulateGardenBunnies() function, add the following:
7.
count = Random.Range(count*3/4,count +1); // randomize the count number
print("zombie Bunnies = " + count);
Set the litterSize value to 8.
8.
In the Start function, change the call to PopulateBunnies as follows:
9.
int tempLitterSize = litterSize * 3; // increased for first drop only
PopulateGardenBunnies (tempLitterSize); // create new zombie bunny prefabs in the scene
Setting a temporary variable based on the normal litter size keeps the variables that would have to
be changed to a minimum if you were going to adjust the difficulty of the game at a later stage. This
way, you will get a higher number of zombie bunnies the first time the garden is populated.
10.
Click Play a few times to see the adjusted count in the console and in the
scene.
With more critters in the scene, it's time to change a few things. If you want to change things that
can't be set directly in the Instantiate method, you will need a means of identifying the clone right
after it was created. If you look back to the description of Instantiate , you will see that it “returns” a
value, the instantiated prefab of type GameObject. That means you can assign the new prefab as a
value to a new variable of the same type.
Preface the Instantiate line with the following:
11.
GameObject zBunny = (GameObject)
The instantiated object has to be cast as a GameObject before you can assign it to a type
GameObject.
Add the following after the Instantiate line:
12.
Vector3 rot = zBunny.transform.localEulerAngles; // make a variable to hold the current
local Euler (x,y,z) rotation
rot.y = Random.Range(1,361); // assign a random rotation to the y part of the temporary
variable
zBunny.transform.localEulerAngles = rot; // assign the new rotation
13.
Save the script, and click Play.
The critters are nicely random in their orientation (Figure 7-33 ).
 
Search WWH ::




Custom Search