- Home /
Health bar by percentage? (not drawing a box)
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!
Answer by Eric5h5 · Nov 14, 2010 at 07:39 AM
I read your example already, but while it can probably be altered for my purposes, I don't understand it because there really isn't much there for my to go on. If I had your project, then I could probably figure it out, but its just to big of a leap for me to grasp.
Answer by Tetrad · Nov 14, 2010 at 07:39 AM
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.
Unfortunately I don't want to use an Orthographic camera :/
You don't have to have your game use the ortho camera. You can use a separate camera for the UI elements and overlay that on top of your gameplay camera.
Answer by The_r0nin · Nov 14, 2010 at 02:17 PM
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.
Hope this helps...
Answer by HROP · Dec 01, 2010 at 11:27 PM
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.