- Home /
Question by
ReedMon · Nov 09, 2012 at 02:19 PM ·
guiflashlightbatterypower
How to make a GUI to show how much battery is left on Flashlight?
How would I make a GUI to display how much battery/power is left on my flashlight? Here's the code i have for the flashlight.
//Name this script Flashlight and attach it to your player for instance
var lightSource : Light; //Connect the light source in the Inspector
static var energy : float = 100; //The energy amount of the flashlight
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var drainSpeed : float = 2.0; //The speed that the energy is drained
function Update () {
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
turnedOn=!turnedOn;
if (turnedOn && energy>0) {
TurnOnAndDrainEnergy();ow how much batter
} else {
lightSource.enabled = false;
}
}
//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn && energy>0) {
energy -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int) {
energy = Mathf.Clamp(energy+amount, 0, 100);
}
Comment
do you mean a graphical GUI or just a simple text display?
Like, there could be a bar, and that bar is filled at full once u start the game but depletes once u use the flashlight
Accidently wrote TurnOnAndDrainEnergy();ow how much batter
Don't $$anonymous$$d it
Supposed to be TurnOnAndDrainEnergy();
Best Answer
Answer by Benproductions1 · Nov 10, 2012 at 07:34 AM
This will answer your question: http://answers.unity3d.com/questions/14430/another-gui-question-healthbar.html
I hate one line answers!