Hello, this is Youngho Lee
In this unit, we’ll learn about player hit detection and handling player death
First, we’ll implement player health management
To do this, we’ll develop the logic for player hit detection
and then learn how to display this information in the UI
Additionally, we’ll explore
how to create getter and setter properties to handle health values
which will improve accessibility and readability
Next, we’ll implement the logic for handling player death
We’ll cover how to control the animation blueprint
so that the death animation can play
regardless of the player’s current state
Implementing Player Health
Let’s implement the player health system this time
To implement the player health system
I’ll add members related to player health
Let’s move to Visual Studio
In Visual Studio
let’s navigate to the NetTPS character header file
Let’s navigate to the NetTPS chracter head file
I’ll add health-related properties
and members at the bottom
Let’s move to the public section
First, let’s define the player health property
with the attributes EditDefaultsOnly, Blueprint, and ReadOnly
Should we categorize it under HP?
Next, let’s define another property
as a float and name it MaxHP
Let’s set its value to 3
Next, we should ensure that
the current health resets to MaxHP
whenever the player respawns
or needs to be initialized with full health
Next, let’s set up the current remaining health like this
Let’s define it as a UPROPERTY with BlueprintReadOnly attributes
We’ll categorize it under “hp” as well, just like before
Should we use lowercase for float HP?
Let’s set HP equal to MaxHP like this
Let’s also add a UI value
that displays this HP
to our character
Since the player
appears on the screen
you can add the HP display
to the player’s head
the top left corner of the screen
or in the HUD (head-up-display) area
above the head
If you’re implementing two modes
placing it in the top left corner would be for the main character’s display
Next, in our multiplayer scenario, there’ll be other users
I want to display
their remaining HP here like this
So, let’s add another HP value
to use in the UI related to the HP that this user currently has
Let’s move to Visual Studio
Let’s navigate to MainUI.h
We need to a member here to display the player’s health
This member will be used for UI display purposes
not the actual HP used by the player
I’ll add the health-related section here under public
and organize it accordingly later
Let’s add it as UPROPERTY (EditDefaultsOnly, BlueprintReadWrite)
and let’s set the category to “hp” for this property
We’ll set the value as float and initialize HP with a default value of 1.0
Why? As I showed earlier
there will be an HP bar displayed above the character’s head
This is ProgressBar widget
and the starting point of this ProgressBar widget is 0
The endpoint is 1.0 It’s based on percentage
so to have this player’s HP fully filled at 100%
I’ll set it to 1.0
Alright, let’s create a UI widget to use this HP
Let’s switch over to Unreal Editor
and let’s navigate to WBP_MainUI in this NetTPS widget
We’re inside WBP_MainUI now
Next
let’s add a ProgressBar widget here to represent the user’s HP
If you search for “progress” in the palette
you’ll find the ProgressBar widget
Drag and drop this ProgressBar
as a child of the Canvas Panel in the Hierarchy panel
Then the ProgressBar will appear here
Let’s name this ProgressBar
“HealthBar”
“HealthBar” it is
Let’s position and layout this HealthBar
Alright, let’s anchor it
to the top left corner in the Details panel
by selecting the anchor point Left Top
Then it’ll be anchored to the top left corner
and let’s position it with X=10 and Y=10
to offset it slightly from the top left corner
And let’s set the size with a width (X-axis) of around 570
Next, let’s set the height (Y-axis) to around 50
This way
the player’s HP will be displayed like this at the top left corner
Then, to determine
how this ProgressBar will react
and what percentage it will fill
you can find the % under ‘progress’
in the right Detail panel
There’s a part about the % of progress
If this value is 100%, or in other words, if it is 1, it’ll be completely filled
If I set the value to 1 here, it’ll all turn blue
The filling of this color is under “Fill Color and Opacity”
If you change this value, you can change the color
We’ll leave it as the default
Whether you set the % value to 0 or not, if you set it to 0, it’ll all disappear
If you set it to 0.5, it’ll be half filled and visible
You can create it like this
So, what will the % value here respond to?
It’ll respond to the HP value
we created in Visual Studio
When you bind it here, the current HP won’t show up
So, let’s build the HP value
that we added in Visual Studio once
After a successful build
go back to Unreal Editor
select the Progress Bar, then choose Health Bar
and on the right side, there should be a binding for %
This binding links them together
When you press bind
you’ll see our added HP value showing up
which will link the ProgressBar’s % value to it
making it responsive
So, we can make the ProgressBar’s % value
responsive to this HP value like this
Got it? Now, first press compile
Now that you’ve compiled, let’s go ahead and run it
When you run it
you’ll see the ProgressBar displayed in the top left corner
and you’ll see it filled completely in blue at 100%
Alright
so you’ve seen your health displayed in the top left corner
Let’s try displaying the opponent’s health next
We said we’ll display the opponent’s health above their head
For example, placing it like this
Here, the opponent will also have the same character as me
In network multiplayer
I’ll temporarily set up the BP Third Person here
and place it for testing
And should I rotate it to face towards me?
I’ll add the HP bar
above this character’s head like this
Like this, above the head, as shown
So, currently, mine is displayed in the top left corner
but for others
we’ll add logic to display it
above their heads here
So, to create the HP bar displayed above the head
let’s first define the content
for this head-mounted HP bar
as a widget in a C++ class
Select the C++ class folder
right-click, and choose “New C++ class”
Next, in the new class wizard
inherit from UserWidget by selecting it as the base class
Next
Let’s name this C++ class “HealthBar”
Create class
Go ahead and click “Reload All” to refresh everything
This HealthBar should have the same attributes
for health as the one in the MainUI
So, you’ll drag this part
select it in MainUI.h
press Ctrl+C
and paste it into the header file of HealthBar.h like this
So, the HP shown in MainUI represents itself
when displayed in MainUI
Then, in the HealthBar
when it’s for someone else (not me)
it’ll represent their HP
Alright, let’s create a widget
that inherits from this HealthBar class
First, let’s build once
and then switch over to Unreal Editor
Alright, let’s go into the UI folder here and create a new widget
In the User Interface section, select Widget Blueprint
Now, in the parent class, choose “All Classes”
If you search for “HealthBar,” you’ll find the HealthBar class that we created here
Select
and let’s name it “WBP_HealthBar
Double-click to open it up
Alright, let’s add a ProgressBar
from the Palette here as well
Then, drag the ProgressBar from the Palette
and drop it into the Hierarchy here to register
The screen appears, right?
So, since we’ll attach this above the user’s head
we’ll need to attach a component to the user
We’ll attach it to the Widget Component
so we won’t attach it directly to the canvas like this
Let’s place the ProgressBar here
Next, select the ProgressBar, then on the right side
you’ll need to bind it to respond to the corresponding values
In the percentage section here, you’ll find the binding option
Just select HP here to bind it accordingly
To do that
select the ProgressBar in the widget, then on the right in the Details panel
choose bind next to the percentage and select HP
For HP to appear
ensure that HealthBar is currently selected as the parent class in the upper right corner
Like this, right?
Because we declared HP as a member in HealthBar
that’s why it appears here
let’s press compile
Then, let’s add a component to the character
so that this HP can react
accordingly
To display this component above the head
we need to switch back to Visual Studio
let’s go to NetTPSCharacter in Visual Studio
Let’s add a component to the character
Alright, let’s add
UWidgetComponent at the bottom
with UPROPERTY and VisibleAnywhere class specifier like this
And let’s name it hpUIComp
Let’s attach the HealthBar widget we just created
to this component
Okay, let’s proceed to the constructor for this
Let’s move the constructor section of NetTPSCharacter
Let’s add this content
to the very bottom
of the constructor function
This is for the HealthBar component
Let’s add it
hpUIComp is added using CreateDefaultSubObject
with UWidgetComponent
Let’s name it TEXT HealthBar
Next, let’s attach the HP bar
as a child of the mesh using hpComp
I’ll put it as SetupAttachment(GetMesh())
Next, at the very top
we’ll add a header
to use this HP widget component
In the #include section
there should be a widget component
in the Comp component
We’ll add this component
and add the WidgetComponent.h under the include component
Done
If you have trouble finding the WidgetComponent header file
you can check where it is located
or you can search for it on Google
If you Google it
you’ll find the WidgetComponent
If you click and check
you can find which header contains the WidgetComponent
You’ll see the header there
It says to include it in the #include section
You can press Ctrl+C to copy it
Alright, you’ve attached the hpUIComp.h component this way
After doing this, let’s build it
Once the build is complete
and you’ve added the new components
go ahead and restart Unreal Engine Editor
Let’s restart it again
The build error is likely due to lingering issues in memory
If there are people working with older versions
just a quick note
in previous versions
like Unreal Engine 3.2
there might have been issues
like this
If you look now, there’s a Build.cs file
In Solution Explorer, you’ll find the NetTPS.Build.cs file
and if you open it, you’ll see the section where modules are registered for use
If you’re trying to use a widget here
you should add the UMG module
by appending it with a comma
in the PublicDependencyModuleNames section
However, right now
we shouldn’t encounter any compilation issues even if we don’t add that
It’s already built-in as of version 5.3, so there shouldn’t be any issues related to that
So, if you’re encountering issues in older or lower versions
and wondering why
it’s because
we’re currently targeting version 5.3.2 for compilation
Versions 5.3.2 and above should not have any problems
but if you’re working with older versions
you’ll need to explicitly add the UMG module in your Build.cs file
As you saw earlier, it’s mentioned as module.umg there
This module needs to be added to that section
In version 5.3.2
this module is already included by default in the Build.cs file
so you don’t need to worry about adding its contents separately
Alright, since the build was successful and Unreal Engine Editor is open
I’ll open the Player BPThirdPerson Blueprint
Currently, our character looks like this
Let’s add a widget to display HP
above the character’s head
On the left side, if you go to the Components panel
you’ll find hpUIComp there
On the right side in the Details panel
you should see a section where you can register the widget component under HealthBar
In the User Interface section
there’s a Widget class section, right?
Here, it currently says “None”
Select this section
and choose our WBP HealthBar widget
that we’re using
It’s done
Then WBP HealthBar will appear like this
But it looks like the size is too large
correct?
So, let’s adjust the Draw Size settings for it now
The draw size settings are right below
Should we set it to around 100 by 10 for this?
Then if you go down to the feet, it should look like this
The current position of the widget component seems a bit off
Let’s adjust the widget component’s position as well
Select hpUIComp HealthBar in the right Details panel
then in the Transform section, set the Z value to 200 to move it upward
Then it should now appear above the head
Since it’s partially rotated, let’s rotate it 90 degrees
By rotating it 90 degrees around the Z-axis
the HealthBar will face forward
Compile it now
Let’s run it to see how it looks
Then it should appear above the user’s head now, right?
You can see
how the HP bar is represented
Now that we have the HP bar set up
we need to add functionality
to handle getting hit
when the user is shot
So, let’s proceed to add functionality
to shoot the user and handle the hit response
Let’s switch over to Visual Studio
Let’s navigate to NetTPSCharacter
Let’s register the hit handling function
at the bottom part
and add void DamageProcess as the function name
Alright, let’s add DamageProcess and implement its functionality
You’ve added DamageProcess to NetTPSCharacter.cpp file
Alright, the task here is straightforward
If the user shoots me
and I get hit, then I should decrease the health
We’ll do this right away
You can decrease the HP by subtracting from its current value
Let’s simply reduce the HP value
When will this DamageProcess function be called then?
Where does it happen when the user shoots me?
They’re going to shoot me when they fire
so let’s go to the fire function
Move to the fire function
In NetTPSCharacter
if you scroll down to the fire function
you’ll see that it mentions displaying particles at the hit location
Shall we add it right below this section?
Let’s handle damage processing here
if the target is an opponent
If the shot hits the ground or a wall
the opponent shouldn’t bleed or fall down
I’ll set it up
so that damage processing occurs only when the target is oneself or a Pawn
There are several ways to validate this
You can detect the target by its name
or by casting it to see
what it hits
If you check the HitInfo
you can use GetActor to retrieve the hit actor
You can cast this actor
to determine its type
How?
NetTPSCharacter
To determine if the actor is of type NetTPSCharacter, you can cast it to check if it’s the opponent you want to process
So let’s proceed with casting it to the type
Use auto and Cast to attempt casting to otherPlayer
I’ll cast it
to ANetTPSCharacter here
If the casting is done correctly
the value will be assigned
If it fails to cast to the NetTPSCharacter type, it’ll be null
So, if otherPlayer exists, the type casting was successful
I will call DamageProcess
on otherPlayer
I’ll call DamageProcess like this
Let’s build it next
Alright, it’s done. We have now modified the hp value here
However, the hp value we modified is
actually the hp in NetTPSCharacter
For the UI to update
the hp in the healthbar needs to be refreshed, right?
Or the hp in the MainUI needs to be updated
or in any case, the hp value in the UI class needs to reflect the changes
but currently, we’re modifying the hp in the NetTPSCharacter
They’re different sets of data
They have different data sets
My name and everyone else’s names are different, right?
The variable name is the same, but the values inside are different
Changing my name
doesn’t change your names
So, think that as this kind of format now
So, to ensure that these values are updated correctly
we need to update the values in the UI
that contain this hp as well
Then I need to add this content here
and we can add it when DamageProcess is called
For example, we would do it like this
Let’s implement it here
I’ve reduced the hp Now let’s calculate the float percentage after reducing it
Calculating the % to assign to the UI
If we set float %
and divide hp by MaxHP, what happens?
We have an initial value of
3 for hp, right?
It’s 3 now
Then, when MaxHP is 3
float % becomes 1, representing 100%
Then, if it’s currently decreased to 2
then, it would reflect the decreased percentage
I’ll calculate it in this way
Alright, then if there’s
a MainUI
we’ll insert the percentage
into the hp
on the MainUI
Using MainUI means referring to itself
For the opponent, things like crosshairs or bullets
don’t need to be displayed
Here, you can see that I’ve played
When I’m playing
my bullets are displayed like this
and I can also show my hp at the top like this
But for the opponent
it’s enough to display things like their headshot indicator
there’s no need to show details
like remaining bullets at the bottom, right?
So, for them
while they’re using the same class
only my side needs MainUI, right?
The opponent doesn’t need MainUI
So, if there’s MainUI, it’s for me
If there’s no MainUI, we’ll distinguish it as being for the opponent
So, we won’t create MainUI widget unconditionally
we’ll create it only when it’s for myself
When it’s the opponent’s turn, we’ll ensure that the MainUI widget isn’t created
Alright, let’s handle the cases where MainUI exists and where it doesn’t
by writing it separately
If MainUI exists, then when my health decreases
we’ll set up an “auto hpUI” and cast it to
the HealthBar on my head, which is UHealthBar
We’ve registered hpUIComp with get-widget
we should link hp from hpUI
to correspond with this percentage here
We set the hp value according to this percentage
You need to include UHealthBar at the top
Please include the HealthBar header file using #include
In the NetTPSCharacter.cpp file
as mentioned earlier, MainUI isn’t created unconditionally
It’s only created when it’s for oneself
So, the place where MainUI is created
is in InitUIWidget
You’re creating MainUI in InitUIWidget right now
You need to handle
some conditional branching before InitUIWidget
One way to distinguish whether it’s the main character or the opponent character is
by using methods like this
For the player
how it’s structured is
typically visible in the right-hand outliner
If I search for “PlayerController” here
I can see that there’s exactly one PlayerController in the world
What do we call these characters we see on the screen now?
We call these characters Pawns, and this is also a Pawn
Oh, isn’t it a character? Doesn’t it inherit from Character?
Characters also inherit from Pawn
So, we refer to it as a Pawn
However, this Pawn doesn’t move independently
it’s set up to receive user input
and process actions accordingly
So, it’s set up to receive user control
So, PlayerController is responsible for receiving and handling user input
It directly represents you and others
The object that represents the user in this virtual space is the PlayerController
The PlayerController directly possesses this Pawn
When we possess this Pawn
we finally gain control over it
This structure means that since it’s ours
I directly possess it
and move it forward, backward, left, and right by pressing the arrow keys
Right? I can shoot the gun
but since I’m not possessing this guy
I can’t control him
This is typically how we use AI for control
So, currently, it doesn’t have a PlayerController
but this does have a PlayerController
Then we can distinguish whether this is me or not
based on the presence of the PlayerController
So if the PlayerController is present, you create the MainUI
and if the PlayerController is absent, you create this instead
Then let’s switch back to Visual Studio
In teh InitUIWidget, if the player is not in control
do we process the section below? No we don’t need to
Because we’re not going to create the MainUI
I will ensure that it doesn’t process if the player isn’t in control
First, declare auto PlayerController pc
Then, cast it as APlayerController
I’ll get the Controller object
This Pawn is also said to be a NetTPSCharacter
When the Pawn is spawned
it’s curently set to
automatically possess the PlayerController
So, because of that setup
the variable “Controller” here has a value assigned to it
But if it doesn’t have a value or if something like an AI controller is assigned instead
then it’s not a PlayerController
So, here it won’t have a value
If the PlayerController is properly assigned
pc will have a value
So, if pc is a null pointer
then we’ll implement it so that we won’t process the following content
in the return statement if pc is null
Oh, then what atbout the healthbar?
Since I’ve just attached it to myself as a component, it’s definitely there
That’s how it’s set up
Let’s go ahead and give it a try by playing it first
I’ll go ahead and build it first, then we can play
Press Play
Go grab the gun and shoot
Hmm, that does sound odd
Let’s try reloading
and aiming at the target again
It feels like it’s hitting the back instead of the front, right?
It seems like it’s hitting the wrong side
like hitting the wall or the opposite side of the body
Why is that happening?
Let’s try the capsule component of this BPThirdPersonCharacter when we shoot
Select the capsule component
then scroll down in the details panel on the right
until you find the “Collision” section
There’s a collision presets section in the collision settings
Let me expand that for you
Under “Collision Presets”
you’ll find Trace Response under Collision Response
The Trace Response in the Collision Presets
determines how the capsule component reacts when the gun fires
I’m performing a line trace with visibility option in the trace channel
to check for collisions
So, currently
in the Collision Presets of the capsule component
the Trace Response under Visibililty is set to Ignore
which means it’s ignoring collisions when performing the line trace
That’s why collisions are not being detected
So, let’s change it to block collisions
How?
In the Collision Presets, select Pawn
from the dropdown and change it to Custom
Now that you’ve selected Custom, go ahead and change it to Block
and compile
go grab it again and shoot
Then you should be able to confirm that the health points decrease correctly
So now you should be able to see the health points decreasing properly
If you want to
change the hp value
in a more sophisticated way
let’s briefly move to the DamageProcess
When we move to the DamageProcess, we decrease the health value
it’s processed there in this way
Is there a way we can make this value a bit more elegant?
In DamageProcess, changing the hp
in a way that automates everything up to that point
and this is commonly referred to as
using getters and setters without directly manipulating the property
If we refactor it using getter and setter functions
to handle it
we might be able to make the code more sophisticated
This is a tip
so let’s work on creating the content here
Typically provided in C# language
now in Windows C++
it can be used
because Unreal Engine is also using the same grammar
So, using this approach
let’s create getters and setters
to modify the hp a bit
If you go to NetTPSCharacter, you’ll find this section here
Currently, the hp are set up like this
but you prefer not to directly manipulate the property
If you want to prevent
direct manipulation of this part
you can set it up like this
Since this is within its own clasd
it doesn’t matter whether you place private or public
However, problems may arise when trying to access it from other classes
Restricting access is done by
marking it as private
This way, other classes will be constrained from accessing it directly
Since we’re working within the class itself, let’s leave it as is for now
Let’s create getter and setter methods
so that we don’t directly access the hp variable
This is a helpful tip, so when you work on other projects
you might find it useful to use this approach
Alright, let’s add the getter and setter right below the hp variable
There’s a thing called decl spec
You can register a getter for this property
by using underscores and the declspec
then adding the property, and then putting a get
If you define GetHP like this
it means you want the function named GetHP to be callable
If you add a comma after GetHP, it signifies that you’re now defining the setter
So here, you’re associating it with a function named SetHP
You close it with a single closing parenthesis
So, this property with the getter and setter can be used
by specifying what will use get and set
You can also specify float here
by pressing Enter
I’ll use it here
with Float HP, using capital letters
to differentiate it from a variable like hp
you can format it like this
This variable would internally fetch its value
using the GetHP function
When you assign a value to HP
it’ll invoke the SetHP function to separate the handling
Underneath, you would add float GetHP
Go ahead and place it right here
While you could use forceinline to place it here
let’s move the implementation to the separate definition section instead
Let’s define the setter as void SetHP(float value)
There was an error now
because I closed the parenthesis prematurely
let’s remove that closing parenthesis
Let’s remove the closing parenthesis
from GetHP
since it was added by mistake
So, you group the property declaration properly
and then define GetHP
Then, it’s correctly created with the scope resolution operator in front
Let’s also create the SetHP function
In the cpp file, you’ll create the HP property along with the GetHP and SetHP functions
If you look again, you write the declspec like this
_ _declspec, one underscore doesn’t matter
Then property, put GetHP in getter
Next, you put the variable name to be used
So, when you call HP, it invokes the GetHP function here
and setting a value will invoke the SetHP function
with the registered value
You can create and use getters and setters like C# language like this
Let’s see how convenient this becomes
Let’s move to the cpp file
In the getter, simply pass over with returnHP
In the SetHP function, we’ll pass over the part
that was handled in DamageProcess
Okay, so in DamageProcess, we’re only decreasing HP
So, when HP decreases, the SetHP function will handle the value separation
The value parameter will contain the updated HP value when it comes in
If you’re looking at something like
When you do hp --
what happens in the code?
HP is subtracted by 1 from HP
So first, you need to retrieve the current value of HP
On the right side, rvalue retrieves the value
So GetHP will be called here to retrieve the value
And then, the -1 will be applied within the created GetHP
and the value will then be inserted here
That’s how value comes in here
This is typcial usage of a property
So, here we simply assign the value
to HP
Since the modified value comes in as value
we directly assign it to HP
And this lowercase HP here
In DamageProcess, I’ll change the lowercase HP to the uppercase HP
Why? Because the GetHP we’re creating is
using the uppercase HP, right?
The lowercase HP is the variable we originally use
and want to utilize here
So, when you call this
it naturally invokes the getter and setter functions
as structured
Done
Then, when you decrease the uppercase HP
in decreases the health
the getter and setter will automatically be called, allowing this action to execute
This is a convenient feature commonly seen in C# language
which significantly enhances readability and usability
Just by doing this, the function will execute
It gives it a Sphinx-like appearance
It looks like a human with the body of a lion, like a sphinx
It feels like the body and head are mismatched
The head used is treated like a variable
but actually functions as a function that runs in a structure
involving getter and setter functions related to property
Let’s build this
and then re-run Unreal Engine Editor
If you aim and shoot the gun, you should see the health decreasing correctly
You’ve completed handling the player’s health up to this point
Player death handling
Let’s proceed with handling player death
I’ll set up animation handling for
when the player gets hit for player death processing
If you try playing now, even if you shoot until the HP is depleted
the player character remains as is
So, let’s handle it such that when the player dies
the death animation plays first
Select ABP Manny from the animation folder
and open it up
Then navigate to the Animation graph
In the animation graph
select the Main States node
from the Main States state machine
Double-click
Then it should look like this
So, within this, locomotion and such were carried out
Jumping off the ground or landing
these are all handled within locomotion
right here
and motion inside
Alright, so to make sure that
when the player dies
the death animation
plays across all states
let’s add a death state within this
So, whether running in motion, holding a gun
or not, death should be possible
Similarly, the opponent when falling on the ground should be the same
So, to allow death from any state
let’s right-click here
and select “Add States Alias”
And name it “Any State”
Name it “Any State”
and check “Global Alias”
to ensure it can be called from anywhere
So, this will allow transitions from all motions like
jump, fall loop, and land into this state
It allows for signaling from any of those motions
Let’s set it like this, and then you see Asset Browser on the right side
Go ahead and search in the Asset Browser
You will find an animation sequence file names “MM_Death Front 01”
under the search results for “Death”
Drag and drop this into the workspace
and rename it to Die
From any state, when the character dies
it transitions to the “Die” state
Connect the transition arrow
from “Any State” to “Die”
Alright? Then, when does it transition from Any State to Die?
When it dies
Since we can’t know exactly when it dies
if we compile it first, a warning will appear here
Why? Because this condition hasn’t been set yet
To set this condition
I’ll add a member variable to the animation class
Let’s switch to Visual Studio
and navigate to the NetPlayerAnimInstance header file
So here, we need to add the content for when it dies
It should be accessible from Blueprint
so we’ll set it as EditDefaultsOnly and BlueprintReadWrite
and the category will be
MyAnimSetting
Like this, we set bool IsDead to false
This will handle whether it’s dead or alive
With this option and property set
we’ll register it and handle the animation transition accordingly
Let’s build it now
Let’s switch over to Unreal Editor
and double-click on the transition condition button
from AnyState to Die
Here, now that the result value is registered
if you open MyAnimSetting, there should be a variable called IsDead, right?
Let’s drag and drop IsDead to attach it
Next, when we compile
if IsDead is set to true
it’ll automatically transition to the Die animation state
from any state
So we need to set the condition where IsDead becomes true
Let’s switch to Visual Studio
and come to the header file
of NetTPSCharacter
to enable storing the death status here
Death status
AllIsDead now means not dead
When this character’s Hp reaches 0, what happens next?
Let’s handle it by setting it to true
Let’s go inside the DamageProcess
to see when it’ll become dead
Here, when it
enters the DamageProcess function, so if the Hp value is 0 or below, that means it’s dead
If the HP value is 0 or below, then what happens?
Since it means the character is dead, we set IsDead to true
Then we need to synchronize the IsDead variable in NetTPSCharacter
with the IsDead variable in the animation class
There’s the cpp file of NetPlayerAnimInstance
Move to that file
Here, from NetTPS to the NetPlayerAnimInstance class
In the NetPlayerAnimInstance class
if we go up, we find NativeUpdateAnimation
It handles similar content to T
I’ll add the applicatino of death status below bHasPistol
to synchronize IsDead
with the IsDead in the player
ensuring they’re linked
In NetTPSCharacter, when the player shoots
they inflict damage on the opponent
causing them to take damage
So, from the perspective of the opponent who gets hit
the DamageProcess function is called
So, their HP decreases, and if it reaches 0 or below, they’re considered dead
And then in the animation, this value is received
and applied to oneself, completing the structure
Let’s build it
and switch over to Unreal Editor to playtest it
Alright, let’s go ahead and play to see how it works
Go ahead and press the play button, then grab the gun
When you shoot
you should see the death animation playing
So you’ve confirmed that the death animation is working as intended
and implemented the overall functionality for player death handling
Let’s summarize what we’ve learned in this section
Implemented player health management Implemented logic for handling player damage
Set up UI elements for own health and differentiate UI elements for opponents
Created UI components using a ProgressBar widget for self-health in the MainUI
Implemented a separate HealthBar widget to handle opponents’ health when they’re encountered
Created getter and setter properties for the health member variable to enhance flexibility in access
Implemented player death handling and death state animation
Added animation state using State Alias to ensure death animation can be triggered from any state
Set conditions for [is Dead] to become true, triggering the death animation when HP is depleted
The End