Health bar

I have this simple green health bar for the player as below that is made by this script -

#pragma strict

var healthTexture : Texture2D;				// Texture to use for the health graphics
private var globalVars : GameObject;		// Referance to the GameObject with the GlobalVars on

function Awake(){

	globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
}

function OnGUI () {

	var adjust : int = globalVars.GetComponent(GlobalVars).playerHealth * 3;	// Referance to the players health

// Place the GUI at the top of the screen
	GUI.BeginGroup(Rect(Screen.width / 2 - 150,Screen.height - Screen.height + 30,adjust,15));
	GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
	GUI.EndGroup();
}

What I’d like to do is when the bar reduces I’d like the top end moving down with it in a different colour like grey, what would be the best way to achieve that effect ?

8058-grab01.jpg
8059-grab02.jpg

Draw two textures. Either Draw the green over the grey

GUI.DrawTexture(Rect(0,0,maxAdjust,15), greyTexture);
GUI.DrawTexture(Rect(0,0,adjust,15), healthTexture);

alternately draw them next to each other.

GUI.DrawTexture(Rect(adjust,0,maxAdjust-adjust,15), greyTexture);
GUI.DrawTexture(Rect(0,0,adjust,15), healthTexture);

In both cases you would not apply the ‘adjust’ value to the group, but to the Textures themselves.

Think I was just being idle asking this question, here is what I ended up with -

#pragma strict

var playersHealthTexture : Texture2D;		// Texture to use for the health graphics
var backgroundTexture : Texture2D;			// Texture to use for the health graphics background
private var globalVars : GameObject;		// Referance to the GameObject with the GlobalVars on
private var adjust : int;

function Awake(){

	globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
}

function OnGUI () {

	adjust = globalVars.GetComponent(GlobalVars).playerHealth * 3;	// Referance to the players health

	if(adjust < 0){
		adjust = 0;	// So the GUI wont be drawn below 0
	}
	
	GUI.DrawTexture(Rect(Screen.width / 2 - 150 + adjust, Screen.height - Screen.height + 30, 300 - adjust, 15), backgroundTexture);
	GUI.DrawTexture(Rect(Screen.width / 2 - 150, Screen.height - Screen.height + 30, adjust, 15), playersHealthTexture);
}