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 ?
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);
}