Game Development Reference
In-Depth Information
6.
Add the following variables:
public GameObject energyBar; //the battery's energy bar sprite
float baseScale; // the energy bar's base y scale
In the Start function, get and store the energy bar's base scale:
7.
baseScale = energyBar.transform.localScale.y; // get the base scale
The progress bar will be updated at the same time as the text value, so you will call a little function
from inside the Update function.
Inside the Update function, beneath the percentRemaining = (int) ... line,
add the following:
8.
UpdateBattery(); // update the sprite graphic
9.
Block in the function that does the work:
// animate the battery's energy bar sprite to match percent remaining
void UpdateBattery () {
}
10.
Inside the function, add:
// adjust battery's energy bar sprite
Vector3 adjustedScale = energyBar.transform.localScale; //store the sprite's scale in a
temp var
adjustedScale.y = baseScale * (batteryRemaining / batteryFull); // calculate the actual
y scale
energyBar.transform.localScale = adjustedScale; // apply the new value
11.
Save the script.
12.
Select the Battery Life object.
13.
Drag Battery_1 into the Energy Bar parameter of its Battery Health
component.
14.
Click Play, and watch the bar drop in response to the percent of charge
remaining (Figure 9-27 ).
Search WWH ::




Custom Search