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?
The way we've solved this problem in the past was to not use the GUI system and instead use planes and an ortho camera. From there you can do things like scale the geometry, or use some kind of shader to blend with the material so that it gets progressively more transparent.
It depends on whether you want the "empty" part of the health bar to be transparent or have some other color. If all you want is the bar to shrink (and missing health be another color), make a parallelogram texture in yellow and scale it on the x-axis based on how much health you are missing. Then, when you put it over the right end of the bar, the red will shrink towards the left and be replaced by yellow. The last bit of health would need to be a separate curved yellow piece to match the left edge of your bar, but at least you'd only have two textures rather than an array.
If you want the missing health part of the bar to be transparent, then you would want to do the same thing as above, but just with a red curve and a red parallelogram, scaled based on how much health you have.
I haven't tried this, but it should work. The only issue might be with the angles of the parallelogram changing when scaled. In that case, you need to use a box to scale and add a curved texture on the right side and a slanted texture on the left.
Hi I understand most of the code,
however there is one thing I don't get.
How do you create a DriverState type of object,
or any type of object for that matter?
I am getting an error "the name Player1 does not denote a valid type ('not found')
because I assumed I could just put the name of my player game object instead of DriverState, since my max health, and current health status are being held within a script that is atached to the Player1.
If anyone oculd shine some light on this to me it would be greatly appreciated, as I haven't found much instruction in the Unity scripting reference.