- Home /
Health Bar To Change Size
Right,
I'm sure this is simple enough. Basically, I have a plane above my enemy that is green. What I would like to do, is to decrease the size of the plane according to the variable 'Health' as well as change through the typical health colours (green, yellow, orange, red ...).
Here is my HealthBar Script so far:
var HealthBar : Transform; var Health = 0; private var PercentHealth = int;
function Update() { print(Health); }
This is what I reckon:
The Var Health does not start at 100 (thus cannot be a percentage). EG: an enemy might spawn with a lower hit point, like 40. Meaning their health is 40, but I need to convert this into 100% (Percenthealth) and decrease the plane according to the percentage.
So whatever the health STARTS at (eg: 40), will be 100% Health
I hope that makes scene, I think I have over complicated the situation.
Cheers
100% of 3.14 = 3.14. A variable does not require to be 100 at 100% to be able to be described with a percentage. per / cent = per / 100 in words. Any size of pie chopped in 100 pieces , big or small.
Answer by oliver-jones · Dec 10, 2010 at 12:44 AM
Figured it out:
var HealthBar : Transform; private var MaxHealth : float = 100; private var MaxBar : float = 0.2; var Health : float = 10;
function Update() { // --- Change Size Of Bar --- \\ transform.localScale.x = (MaxBar) * (Health/MaxHealth);
// --- Change Colour Of Bar --- \\
if(transform.localScale.x == 0.2)
{
transform.renderer.material.color = Color.green;
}
if(transform.localScale.x <= 0.15)
{
transform.renderer.material.color = Color.yellow;
}
if(transform.localScale.x <= 0.05)
{
transform.renderer.material.color = Color.red;
}
}
(PS - No need to remind me - I will tick this answer correct, but I have to wait 48 hours!)