Game Development Reference
In-Depth Information
2.
Add the code to bring the color to red:
// if less than or equal to 25%, adjust color drop green to get red
if(percentRemaining <= 25 ) {
float adj = (25 - percentRemaining) * 0.04f;
energyBar.GetComponent<SpriteRenderer>().color = new Color(1f,1f - adj,0f);
}
In either case, there is only one of the RGB values changing, so the other is set to 1 or 100%. Blue is
never used and remains at 0.
3.
Save the script.
4.
Click Play, and watch the color change as the battery's charge gets lower
and lower.
To reward your player for eradicating the threat, you will freeze the battery drain when the zombie
bunnies are no longer able to reproduce. In the BatteryHealth script, the trackingBattery variable
controls the battery energy drain, so you will be changing its state from the ScoreKeeper script.
1.
Open the ScoreKeeper script.
At the top of the // stop the population explosion! conditional, add:
2.
// stop the battery drain - the threat is almost neutralized
GameObject.Find ("Battery Life").GetComponent<BatteryHealth>().trackingBattery = false;
3.
Temporarily set its condition to 10 zombie bunnies remaining while testing:
if (currentBunCount == 10) { // stop the population explosion!
4.
Save the script.
An error in the console reports that the variable is not accessible due to its protection level. Once
again you will have to add internal to gain access.
5.
In the Battery Health script, change the variable declaration to:
internal bool trackingBattery = true;
The internal allows you to access it from other scripts without exposing it to the Inspector as would
using public . Functions will have to be marked as public if they are being called directly from other
scripts, as you will discover later in the project.
6.
Save the script.
7.
Click Play, and shoot all but the currently set limit of zombie bunnies.
The battery stops dropping, allowing the player to eradicate the remaining pests at his leisure.
Search WWH ::




Custom Search