Updating the health meter

You will recall that in previous chapters, we linked the meters that are displayed on our Heads-Up Display (HUD) to variables contained within FirstPersonCharacter. In order to show the health meter decreasing in response to taking damage, we must decrease the player health variable each time damage is received. We also want to ensure that the player's health can never go below 0 to avoid potential bugs that an unexpected negative health value might cause. The end result will look like this:

Begin by adding the Event AnyDamage Event node to empty graph space. Drag the Player Health variable onto the graph and use it to create a GET Player Health node. Attach this variable node to a float - float node. Now, drag a wire from the Damage output pin of the Event node and attach it to the bottom input pin of the float - float node.

To ensure that the health value never goes below 0, drag a wire from the output pin of the float - float node and attach it to a MAX (float) node. Leave the bottom input field at its default value of 0.0. This node will return the higher of the two values given as inputs. This way, if the calculated health ever goes below the lowermost value of 0.0, then it will return 0.0 rather than the player's health value calculated after the damage is subtracted. Finally, connect the output pin of the MAX (float) node to a SET Player Health node to adjust the variable value, and thus the health meter display.

Select all these nodes and create a comment that describes the functionality. Then, compile, save, and press Play to test this. You should now notice the player's health meter depleting when an enemy gets within range of the player and stops to attack.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset