Maybe what I am trying to do is not possible. I've done a health bar in the past with an array of images, but I want to learn how to work a health bar based on the player's percentage of health.
Here's a screen shot:
Those are two different images, the border and the actual health bar. I'd like the health bar to shrink towards the left with the percentage of the player's health. Here is what I have at the moment.
// native resolution
var nativeVerticalResolution = 1050.0;
var nativeHorizontalResolution = 1680.0;
var healthBarGUIImage : Texture; //the border image
var healthBarImage : Texture; // the actual red bar
private var playerInfo : DriverStats;
var maxHealth : float;
var myHealth : float;
var percentHealth : float;
function Awake()
{
playerInfo = FindObjectOfType(DriverStats);
}
function OnGUI ()
{
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3 (Screen.width / nativeHorizontalResolution, Screen.height / nativeVerticalResolution, 1));
// This draws the border image
GUI.Label(Rect (1, nativeVerticalResolution - healthBarGUIImage.height, healthBarGUIImage.width, healthBarGUIImage.height), healthBarGUIImage);
maxHealth = playerInfo.capHealth; // gets the player's maximum health
myHealth = playerInfo.health; //get sthe player's current health
percentHealth = myHealth / maxHealth; // find the percentage of health that they have
// This draws the actual health bar.
GUI.Label(Rect (1, nativeVerticalResolution - healthBarImage.height, healthBarImage.width, healthBarImage.height), healthBarImage);
}
I've tried a few things, and haven't got anything that works. I've looked for examples that don't involve arrays of images, but they usually involve just using a box as the health bar, which I don't believe will work with this shape. Is there a simple way to scale this texture down from one side to the other with the GUI?
Thanks!