- Home /
Solved with another question
Oxygen Tank Progress Bar
Is there a way to make a bar made out of a GUI texture slowly scale down along the X Axis over time? I basically have a 3 layer GUI set up where there is an overlay and a background bar that I would like to have scale down slowly over time to indicate that something like Oxygen is running out.
I was looking at how health bars are made but non of them seemed to be the sort of thing I am after. Does anyone know of an easy way to do this?
Answer by Kiwasi · Dec 13, 2014 at 01:57 AM
Are you version constrained in Unity? If not I would upgrade to Unity 4.6 and use a Slider. This makes the job trivial, simple set Slider.value to whatever number you like.
$$anonymous$$aybe but I just need to do something with a simple script as I may add features to it later that seem kind of difficult to do with the new UI feature. For instance I find some elements are hard to layer with the new UI system and some of my animations get buried under the new UI even if I try making them a child of the canvas or panel. So for this screen which is pretty self contained all I need is something simple that will make the bar, in this case a big rectangle behind an overlay to extinguish over a set time, preferably a time I can either set with buttons or just in the inspector for now.
I tried using this idea but I could not seem to get this to layer behind my overlay GUI full screen texture.
static var curHp : float = 300.0;
static var maxHp : float = 300.0;
static var guiDepth : int = 2;
var HpBarTexture : Texture2D;
var hpBarLength : float;
var percentOfHp : float;
function OnGUI () {
GUI.depth = guiDepth;
if (curHp > 0) {
GUI.depth = 2;
GUI.DrawTexture(Rect ((Screen.width/2) - 365, 112, hpBarLength, 255), HpBarTexture);
}
}
function Update () {
percentOfHP = curHp/maxHp;
hpBarLength = percentOfHP*700;
if(Input.Get$$anonymous$$eyDown("h")) {
curHp -= 10.0;
}
}