Solution using WidgetComponent:
Here is the way i always do it:
Create a new Widget and add some kind of representation for health. I assume you will use a progress bar.
Add two 2 floats to your widget AND your character: maxHealth, health.
Add a third float to your widget called healthProgress. healthProgress should default as 1.0 (full health).
Go to your progress bar you added in step 1 and bind it to the healthProgress variable.
Add a function to your enemy character called "modifyHealth" with input parameters of "healthToSubtract". Inside the function simply do: {health - healthToSubtract} and clamp the result to be between 0 and maxHealth and the value of this calculation should be set as the new health value.
Add a function to your widget called "UpdateHealth" that gets a Float as input. in it, simply drag a Set for healthProgress and attach to it a "Map Ranged Clamped" node. as the value, attach the float parameter. as In Range A put 0. in In Range B attach maxHealth. in Out Range A put 0, in Out Range B put 1.
Go to your enemy character and add a "WidgetComponent" to it.
set the Widget in the WidgetComponent to be the widget you created in step 1
Move it in the character's relative space to be where you want it to be. usually above head is good enough.
in your enemy character's EventBeginPlay, drag the WidgetComponent to the event graph, drag its pin and choose "Get User Widget Object". drag from it and do "Cast to ". Promote the cast result to a new variable. the variable should be of the same type of the widget you created.
In your enemy character's EventBeginPlay, drag a Get for the widget variable saved in step 10 and set it's health and maxHealth to be the same value you set for these variables in your Enemy Character. Also, call the widget's Update health and connect health to it (to make sure it starts with the right value)
When your enemy takes damage, use ModifyHealth to decrease it's health. Afterwards drag a Get for the widget variable saved in step 10 and call "UpdateHealth" and connect health as the float parameter.
The widget will now update the progress bar and show the health.
BONUS: to make sure the progress bar is always facing player, in the enemy character add a function called "rotateHealthbarToPlayer" in it, do the following:
right click on event graph and look for "Get player camera manager". from it, drag the pin and look for "Get camera location".
Drag widget component to event graph and from its pin drag and look for "get world location".
right click on event graph and look for "Find look at rotation". as From connect the camera manager. as To connect the widget location.
From widget component drag pin and look for "Set World Rotation" and set it to the result of Find look at rotation.
Call this function from Event Tick.
To sum up: Enemy character loses health. It updates widget to display it in progress bar (which is in a value of 0 to 1). Widget will rotate towards player each tick.
If i missed something, let me know. Tried to be as specific as possible.
All the best :)
THANK YOU for CopperStoneSea for finding a mistake in step 10
answered
Mar 28 '17 at 11:33 AM
DDraiman1990
146
●
4
●
5
●
8