Only show health bar if the player is close to the enemy and can see it?

I have enemy characters with a widget component attached and a process bar, i choose the UI Screen but the problem is that it always shows the health bar no matter the distance or if the enemy is behind something, i want to only show it when the player can see the enemy and is close to it, lets say something like 5000 units, how can i do that?

1 Like

You can try these out.

  1. You can use a Get Distance To Node on your enemy blueprint, but might need to hook up to an event tick or pawn sensing.

  2. One big collision sphere on your enemy blueprint. If player runs in sphere turn on health bar and off if out of sphere.

Hope this helps.

Do it the other way around … add the sphere to the player rather and fire it off that overlap.

I did the reverse of that for one of the Unreal Game Jams so that when the enemies were far away, you’d see an indicator above their head showing where they were. This is only a partial answer to point you in the right direction but here is the blueprint code we used.

Here’s the video of the game we used it in, about 30 seconds in: [There Can Only Be One - UE4 Game Jam June 2015 - YouTube][1]

There might be a better way to do it but this is our solution. This code works for visible far/invisible close so you might just need to take the value after the clamp and subtract it from 1.

I didn’t catch if you were using UMG or what, but you’ll have to tie the resulting float value into the opacity of the healthbar. If you need more help, let me know and I’ll see what I can do.

1 Like

If you’re going for simple, the other answers would be better lol

I prefer the enemy because if you have multiple enemy you have to set each different class on the player blueprint. While on the enemy there is only one player to look for.

Or you can have it on player and search for every child enemy blueprint. Which works the same. I just prefer the enemy don’t have to cast anything.

I changed it up slightly to also affect scale, so it’d get smaller the further away the enemy was from the player.