- Home /
How To Make A Floating Health Bar
Hello,
I have a load of initiated enemies that come onto the scene. They all have planes above their heads that is green, and when health is deducted, the plane gets smaller whilst changing colour.
How do I make the plane always face the camera 'so it appears like a GUI', and not rotate with the enemy?
(The health bar plane is a child of the enemy)
Unless you have any other ideas.
Here is my health bar script:
var HealthBar : Transform; var MaxHealth : float; var MaxBar : float; var Health : float; var m_Camera : Camera;
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;
}
}
Thanks, Ollie
Answer by Bampf · Dec 13, 2010 at 05:10 PM
Keeping a plane rotated to face the camera is called "billboarding".
http://answers.unity3d.com/questions/13611/how-to-create-billboarding-planes
Okay, Its getting there, but because the healthbar is a child of the enemy, the position goes a bit funny every time the enemy rotates - How do I get it so that the health bar is always on top of the enemy?
A few ways you could do it. 1. You could have the enemy and the bar both be children of an empty child object. You'd translate the parent to move both, but only rotate the enemy. 2. After the rotation of the enemy, rotate the billboard only backwards the same amount. This should position it back where it was, if you use the right origin. 3. Ins$$anonymous$$d of making the billboard a child of the enemy, have it be a separate, unrelated object with a script on it to make it follow the enemy around.