- Home /
How to make Battery Bar?
Yeah i was wondering how would i make a bar so it shows up as a health bar but slowly decrease as time passes by. Any help would be appreciated.
Answer by Lttldude · Apr 16, 2012 at 06:46 PM
Go here http://unity3d.com/support/documentation/Components/gui-Layout.html and scroll down to the example labeled "/ Using multiple Groups to clip the displayed Contents /"
var bgImage : Texture2D; // background image that is 256 x 32 (coud be a different size, but has to match the sizes below (GUI.BeginGroup (Rect (0,0,256,32));)
var fgImage : Texture2D; // foreground image that is 256 x 32
var duration = 1.0; //how long the healthbar will last
private var time : float;
function OnGUI () {
//increase the amount of time each frame, clamp it so it doesn't go higher than duration
time = Mathf.Clamp(time + Time.deltaTime, 0, duration)
// Create one Group to contain both images
// Adjust the first 2 coordinates to place it somewhere else on-screen
//adjust the last 2 coordinates to change the size
GUI.BeginGroup (Rect (0,0,256,32));
// Draw the background image
GUI.Box (Rect (0,0,256,32), bgImage);
// Create a second Group which will be clipped
// We want to clip the image and not scale it, which is why we need the second Group
GUI.BeginGroup (Rect (0,0,(time-duration)/duration * 256, 32));
// Draw the foreground image
GUI.Box (Rect (0,0,256,32), fgImage);
// End both Groups
GUI.EndGroup ();
GUI.EndGroup ();
}
Give that a try and tell me if you need more help.
It's not doing anything all it's doing is it's just showing the background image.
Answer by Lttldude · Apr 17, 2012 at 05:21 PM
minor change:
GUI.BeginGroup (Rect (0,0,(time-duration)/duration * 256, 32));
Your answer
Follow this Question
Related Questions
Flashlight battery indicator 1 Answer
How can i make a GUI for my battery life? 1 Answer
How to sprint and have a stamina bar? 0 Answers
Inverting the cutoff property (UI bar) 0 Answers
Best way to make an animated infinite striped progress bar? 1 Answer