Health Bar common effect

So this is kind of hard to explain, but it’s a very common effect. Basically when a character takes damage the health bar length lowers and there is this other bar under it which fades into the position that the new health bar length is.

For example…
alt text

Unity doesn’t allow me to create 2 GUI.Boxes in the same OnGUI function so how can I achieve this effect?
Please help!

You can do this by creating two GUITextures

The first is the empty bar. The second is the filling. Make the filling of a size to fill the bar, and then scale it at run time to make it smaller.

http://docs.unity3d.com/Documentation/Components/class-GuiTexture.html

I finally figured it out after reading through

http://forum.unity3d.com/threads/193247-Free-Health-Bar-script

my script now goes as follows for JS:


var curHP : int;
var Health : int = 100;
var maxHealth : int = 100;
var healthLength : float;
var healthunder : Texture;
var healthover : Texture;
var damping : float 10.0;

function Start () {
healthLength = Screen.width/4;
}

function OnGUI () {
var previousAdjustValue : float = (curHP * healthLength) / maxHealth;
var percentage : float = (Health * healthLength)/maxHealth;

GUI.DrawTexture (Rect(10,10, (previousAdjustValue), 20), healthover);
GUI.DrawTexture (Rect(10,10, (percentage), 20), healthunder);

}

function Update () {

if (curHP > Health) {
	curHP -= ((maxHealth / Health) * (damping)) * Time.deltaTime;
} else if (curHP < Health) {
	curHP = Health;
}

    if (Health <= 0) {
Death();
Health = maxHealth;
}

    if (Health > maxHealth) {
Health = maxHealth;
}

if (maxHealth < 1) {
	maxHealth = 1;
}

AdjustCurrenthHealth(0);

}

function AdjustCurrenthHealth (adj : int) {
Health += adj;

healthLength = (Screen.width / 4) * (Health / maxFloat);

}

if anyone is having the same problem I hope this helps!