- Home /
Flashlight battery indicator
Hi, as I said in my previous question, I'm new to Unity. I'm making a slender rip-off (yes, antoher slender question). I'm having trouble to make a bar that displays how much battery is left on the flashlight. I have an indicator, but it is in numbers so I want it to be a bar muck like a health bar
If anyone's interested, here's the script for the battery indicator I have at the moment:
pragma strict
//Makes you write with stricter formatting, which in turn increases the efficiency of the script
//Batteries
var batteries : int = 0;
//How much charge per battery
var charge : float = 100;
private var chargeDisp : float = 1;
/*^^ this is just used to display the variable through GUI.Label -- when a (Long) variable changes
many times, very fast, unity cannot keep up with redrawing the variable at that speed,
and so the text flashes. I don't like the look of it. This converts it down
a couple of decimal places, which makes it easier on the engine. I like how it looks at 2
decimal places. Convert it to 1, for no decimal places, and 100 for two decimal places.
1 is the number, 0 is a decimal place*/
//The actual torchlight object
var torch : Light;
//Speed at which the light decreases
var speed : float = 1;
//Used to turn light on or off
private var lightState : boolean = false;
function Awake() //Called before anything in a script, even called before start
{
torch.intensity = 1.85; //Set the intensity to 1 (1 is defined as 100 charge)
}
function Update ()
{
chargeDisp = Mathf.Round(charge * 100) / 100; //See line under variable declaration
if(lightState && charge > 0) //If mouse has been pressed, and there is still charge in the battery
{
torch.enabled = true; //Turn on the torch
charge -= Time.deltaTime * speed; //Decrease the charge
} else { //If one of the terms has not been met (IE, mouse isn't pressed or there is no charge
torch.enabled = false; //Turn the torch off
}
if(charge <= 0 && batteries > 0) //If charge is less or equal to 0, and if batteries is above 0
{
batteries = batteries -1; //Take on from the batteries
lightState = !lightState; //Toggle lightState
charge = 100; //Charge equals 100 (This simulates putting another battery in the torch. 1 less battery, full charge
}
if(batteries == 0) //If there is no batteries
{
charge = 0; //There is no charge. This stops 'Batteries 1', 'Charge 100' from happening
}
if(Input.GetMouseButtonDown(1)) //Called when the mouse right click is down
{
lightState = !lightState; //Toggle lightState
}
if(charge < 4) //If charge is less than 5
{
torch.intensity = charge; //The torches intensity equals its charge. This makes the torch brighter, but then it fades out pretty quickly
}
}
function OnGUI()
{
GUI.backgroundColor = Color.green;
GUI.Button (Rect (10, 10, 100, 20), "Bateria:" + chargeDisp ); //Display for charge.
}
Could you guys help me?
Answer by robertbu · Jan 11, 2013 at 10:36 AM
There are several ways of making an indicator. Without knowing more about your goal, it's hard to give specifics. Here are a few:
Have a small green plane on top of a red plane. Move and/or size the green plane based on the charge.
Have a series of game objects each representing a level of charge. Based on your charge, set each one to on/off/red/green/light/dark. This can be done by either swapping the textures or with some shaders, simply setting a the renderer.material.color
property.
Create 11 textures...one for every 10% from 0 to 100. Swap the textures based on the charge level.
Set the pixels of the texture for the indicator directly.
Your answer

Follow this Question
Related Questions
Recharging battery script, help 1 Answer
Flashlight battery script 3 Answers
Battery Pickup Regeneration 2 Answers
How to make Battery Bar? 2 Answers
-light.intensity, light.spotAngle and light.color with variable? 0 Answers