Game Development Reference
In-Depth Information
The current zombie bunny count is now better behaved, but the
loadRate
continues to be an issue.
Let's use a similar tactic on the code that sends the message to implement the reward. You've
already got the code that checks the reload value. Once the minimum value has been reached, there
is no reason to carry out the rewards code. Because you are dealing with floats, it is safer to test for
less than 0.2 than == 0.1 in the conditional.
In the ScoreKeeper script, just above the
// manage player rewards
section,
add the following:
1.
if(launcher.loadRate < 0.2f) return; // no rewards available
Comment out the
print
line in the
// manage player rewards
section.
2.
3.
Save the script, and test again.
This time, the potato launcher's reload rate stops short of a serious cooking oil fire.
With the task of eradicating the zombie bunnies becoming attainable, it's time to give the poor player
another break. Because it can sometimes be difficult to locate the last zombie bunny standing, you
are going to at least stop the zombie bunny procreation when there is only one left. For the sake of
argument, you can consider zombie bunny procreation and birth to be instantaneous, so if there is
only one left, the player will not be plagued by more of the ravenous creatures.
If you think back to when you were populating the garden, you may remember creating and using a
canReproduce
variable that is checked before
PopulateGardenBunnies()
can be called after the first
time. This is how you will stop them once the target number of 1 has been reached.
1.
Open the ScoreKeeper script.
2.
Add the following variable to identify the SpawnBunnies script:
public SpawnBunnies spawner; // the SpawnBunnies script
Above the
if(launcher.loadRate < 0.2f)
line, add the following:
3.
if (currentBunCount == 1) { // stop the population explosion!
spawner.canReproduce = false;
}
4.
Save the script.
An error appears in the console (Figure
9-2
).
Figure 9-2.
Protection-level error for the
canReproduce
variable
As with the
loadRate
variable, the
canReproduce
variable must also be made available to other
scripts, but not to the Inspector through the use of
internal
.