Games will often need the ability to give health to players and/or enemies and players need to have the ability to gain and/or lose the health. In this recipe, it being the first of a two-part recipe, we will create the ability to take and heal damage, whereas later on in Chapter 10, User Interface, we will use the UI tools in Unreal to display this information.
Before we start working on this, we need to have a project created and set up for our character to actually have health and a way to damage it. Follow these steps:
Cookbook_Chapter8
). Once you are done, click on Create Project.Now, once the project is created, the first step here will be to create some action to damage the player. In this instance, I'm going to create a trigger. When the player touches the trigger, it will get damaged. To make it easier to see, I'm going to add a fire particle system. Follow these steps:
StarterContent/Particles
folder and drag and drop the P_Fire
particles onto the floor of the starting level.Now that we have the trigger in place, we need to create a blueprint to do the damage for us.
This event will be called whenever any other actor begins to overlap this actor.
For more information on OnActorBeginOverlap and all of the other Collision Responses in UE4, refer to https://docs.unrealengine.com/latest/INT/Engine/Physics/Collision/index.html.
Apply
and when Apply Damage is selected, press the Enter
key.10
.What this will do is whenever an object touches the trigger volume, it will call a generic damage event on that object with a damage parameter of 10
. Note that this will only do something if someone has created an AnyDamage
event, which we will learn how to do later on in this recipe.
Now that we have something that can damage the character, we need to actually give our character a way to take damage:
ThirdPersonBP/Blueprints
folder and double-click on the ThirdPerson Character
object to open it up.CurrentHealth
) and then from the Details tab, change Variable Type value to Float
.MaxHealth
.100
to each of the two numbers.This event is called if the ApplyDamage
function is called on an object. The default character controllers do not have it, so we need to put it in.
First, we will subtract the damage from the current player's health and then, set the current health to this new value.
CurrentHealth
variable from the Variables section of the MyBlueprints tab into Event Graph. From the options, select Get.Next, we want to ensure that the value that we get from this, will always be between 0
and our Max Health value (we can have someone later on create a trigger with a negative Damage value for Apply Damage, which will then heal our player). To do this, we can use a Clamp action that will make it so that when the number is less than 0, it will set it to 0 or if it is larger than Max Health, it'll come down to Max Health.
0
, but then for Max, connect our MaxHealth variable (drag and drop and select Get).Now we have the ability to take or heal damage to our player. Later on in Chapter 10, User Interface, we have the second part of this recipe in which we use this recipe and then create a healthbar to show the player exactly where their health is!
We've covered quite a lot, but we have only touched the tip of the iceberg, that is, scripting. To give yourself a little exposure to what else can be done with blueprints, take a look at the following tutorials:
18.220.178.243