Use one Health Bar for several Enemies

Hey buddy, just did this myself too.

The problem in your picture is that you’re using get all actors of class. Which means all actors in that class will update the health value. Lemme show what I got.

So these are some sample enemies with healthbars:

266907-healthbars.png

][2]

As you can see it tracks individual health. I assume this is what you’re looking for?

This is the function for setting the health bar level:

So simple it’s almost stupid isn’t it?

Here’s how it gets sets when enemy is initalized:

It’s set to hidden in game until the enemy has a target because it’s set to rotate with the screen and unfortunately cannot be prevented from showing through walls (you don’t need this in the blueprint, just tick the box for the component). There are workarounds for this but they are time-consuming and I’m lazy. I just do show/hide instead.

EnemyHP is of course my widget blueprint. HP widget ref is a variable I made (drag out as enemy hp → promote to variable). And healthbar is the actual widget component attached to the enemy in the viewport.

I was gonna add how it’s updated when it takes damage but it’s kinda pointless since it’s the exact same (just the health variable is different).

If you have any questions be sure to fire away. The “Max health” variable is totally optional, you can divide by whatever you like to get it as a percentage (health points need to be between 0 and 1 in my version, but you can make it between any values you want in the widget blueprints).

The benefit of this is that it can literally be used for any enemy you have. If it’s a big enemy just scale it up and move it higher in the viewport for example, or you can set it to its feet. Or you can make the widget stick to your own screen if it’s a boss type where you want visible health on screen instead of over the enemy (I’d probably duplicate it and make some visual alterations just for looks in that case). For that kind of thing you’d use add to viewport instead though.

1 Like