Create a Health Bar like in Farcry 2

Im trying to code a health bar sorta like in farcry 2, i’ll put a gif so you can see what i mean.
But i don’t know where to start, i have the health bar sprite though, what do i do with it?

Oh, that’s easy enough.

float currentHealth = 30;
float totalHealth = 100;
float regenChunk = 20;

Update()
{
if ( totalHealth < 100 )
{
float nextMaxChunk = Mathf.Ceil(currentHealth/regenChunk) * regenChunk;
currentHealth = Mathf.Min(currentHealth+regen, nextMaxChunk) ;
}
}

StimPack()
{
currentHealth = 100;
}

But how do i apply that for instance, to this PNG sprite bar that i have?

You can’t. A PNG is a plain image, if you want it to move, you have to use more images and make them move in real time. Otherwise, as @Xelnath suggested, you can code it yourself.

Yeah, you’ll need to create your own UI elements. There’s tutorials on how to make health bars all over the internet.

You use an Image element with its type set to Image.Type.Filled, and set the fillAmount to the fraction (0-1) of fill that you want to display.

You will probably want to actually layer two images: a transparent border/overlay, which always stays the same, and an underlay which is the fill (and that’s the one you set fillAmount on).

If you want this health bar to follow a unit in the world, then just create a little world-space Canvas under the unit in the hierarchy, and put your image elements there.

Why not just create a normal health bar with a graphic with each bar you need on it, then overlay a divider graphic on top of that. That way you don’t have to mess with dividing your bar via code. Simple solution.

Isn’t that what I just said? :slight_smile:

It might be. :P.