So i want the Healthbar to dissapear if there is no change for a few seconds, but I havent been able to figure out how to do that. I searched for this on the Internet but found nothing. I hope you can help me out.
Hey,
I assume you are using Blueprints.
-
Make OnChange dispatcher, function or event.
-
Call it when your healthbar changes.
-
Make disappear animation (or set visibility to hidden but this will be ugly)
-
In Onchange make retrigerrable delay (It will execute after some time (disappear time) and if your health changes it will start counting again.)
-
After delay finishes call this animation from point 3.
Przemek2222’s way is not bad and can get the job done but there is an easier way to do this.
- In your UI make sure that your health bar “is variable” is checked in the top right corner.
- Then in your character blueprint (or preferably your character base), when you begin play create your player hud widget,
- Promote that to a variable
- Then add it to viewport
- Now grab that variable and get your health bar from it
- Get your render opacity from that
- And either set it to zero so its not there when we start
- Or just put a delay in to leave the bar up for a few seconds and then set opacity to zero (kind of abrupt though)
- Or (my personal favorite) add in a timeline, add a float track to it and set it for however many seconds you want it to fade out for.
- Then put in your render opacity
- From that value do a lerp between A at 1 and B at 0 and connect the alpha to be that float track we created.
- Now when you press play the bar should show up for a few seconds, depending on that delay amount you set, and then disappear over time (set by our timeline) from full visibility to no visibility.
Now if you want to be able to use this in several spots simply make a custom event and name it whatever you want for clarity and drop everything from the timeline forward into this event. Then in begin play instead of doing that long list of nodes (which really isn’t that long) just call that new event you made. Now when you go to take damage just grab your hud variable we set earlier get the health bar and set opacity to 1 so we can see it. Then after damage has been done call that custom event again to get it to fade away after a bit.