x


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

more ▼

asked Dec 13 '10 at 04:47 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 254

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Keeping a plane rotated to face the camera is called "billboarding".

http://answers.unity3d.com/questions/13611/how-to-create-billboarding-planes

more ▼

answered Dec 13 '10 at 05:10 PM

Bampf gravatar image

Bampf
5k 8 19 49

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?

http://www.youtube.com/watch?v=dC30FSgsoms

Dec 13 '10 at 07:25 PM oliver-jones

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. Instead 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.

Dec 14 '10 at 03:24 AM Bampf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3689
x3002
x329
x182
x94

asked: Dec 13 '10 at 04:47 PM

Seen: 4570 times

Last Updated: Dec 13 '10 at 04:47 PM