Multiplayer Damage and Health System in Unreal Engine 4

Unreal Engine 4 contains a powerful multiplayer compatible damage system that works with all actor types.

This system easily allows you to create a Multiplayer Damage and Health System in Unreal Engine 4 only using blueprints.

Prerequisites

This guide expects that you understand how to use basic nodes such as branch, get player controller and input actions / events.

If you do not know how to use input actions in Unreal Engine 4 click here to read our guide.

This guide also expects that you know what a line trace is and how to set them up for a first person style game.

If you do not know how to setup first person line traces click here to read our guide.

How Unreal’s Damage System works

How to Deal Damage

The most basic way to deal damage to another actor is to use the “Apply Damage” node.

The apply damage node with the damage set to 15.0

Here are the important variables to set:

Damaged Actor – The actor that you want to damage
Base Damage – The amount of damage to apply to the damaged actor
Damage Causer – The actor that applied the damage to the damaged actor

How to Detect Damage

When added to any actors event graph, the “Event AnyDamage” event node runs every time the actor is damaged.

The AnyDamage event that runs every time your actor is damaged

Here are the important variables to use from the event node:

Damage – The amount of damage that is detected
Damage Causer – The actor that dealt the damage

Creating the Weapon Damage Nodes

We recommend using the default First Person Shooter template for this guide.

This damage system works will all game types but when learning a new system it is easier to have a template that works well before you start.

The First Person Shooter template has a player character setup with movement, mouse aiming, arm and gun models and animations.

Requesting to Shoot to the server

Firstly, to make the weapon damage run correctly on multiplayer, create a new custom event called “Net Shoot Weapon”.

Creating the custom event to fire the weapon on the server

Then click the new event (Net Shoot Weapon) and in the detail panel on the right of the screen change the “Replicates” drop down box to “Run on Server”.

Setting the Net Shoot Event to run on the server

Changing this setting will now run this event from the client to the server. This prevents cheating as all damage and collision checking will be done server side.

The custom network compatible Net Shoot Weapon event
This event called on the client is only run on the server

Next create an InputAction Fire event (included in the first person shooter template) and create a Net Shoot Weapon node to the Pressed pin.

This will send a message to the server to shoot your gun and calculate the

Telling the server to fire when the fire button event is pressed

Finally for this section to be complete, connect the animation and sound effect blueprint nodes from the template to the Net Shoot Weapon execution pin.

This complete section will look like this:

The complete firing code on the client

Syncing the Animation and Sounds

The animation and sound effects running on the previous step don’t currently run for other players.

To play the animation and sound for this player on everyone else’s screens we need to create a multi cast event.

Firstly, add a new custom event named “Multi Weapon Animation”.

Creating the custom event to fire the weapon on the server

And change the “Replicates” to “Multicast”.

Changing the replicates property of the event to Multicast

Create a branch node, a Get Player Controller node and a Get Controller node.

From the Get Player Controller node create an equals node and connect the “Get Controller” node to the bottom pin as shown below.

Preventing the animation and sound from playing twice on the client
This is done to prevent the animation playing twice on the owning players screen.

The last nodes required for this step can be copied and pasted from the step above.

The complete section looks like this:

The complete multicast animation and sound effects event for the clients
Multicast events run for all clients

Server-side Weapon Shooting

Firstly, from the “Net Shoot Weapon” event created earlier in this guide, we need to create a “Multi Weapon Animation” node and connect it together.

This will sync the animation and sound to all clients on the server whenever a player shoots.

Running the Multi Weapon Animation event from the server to sync all animations and sounds for all clients

To damage another actor when the request is received, we first will need to use a Line Trace for Objects node.

Connecting the line trace for objects node to the server shoot weapon event
This line trace runs on the server when called and uses the server’s state to prevent cheating.

If you do not know how to setup a line trace click here to read our guide on first person line traces in Unreal Engine 4

From the line trace we need to create an “Apply Damage” node and connect the “Hit Actor” pin from the “Break Hit Result” node to the “Damaged Actor” pin on the “Apply Damage” node.

A close up of the break hit result connecting to the Apply Damage node

Set the “Base Damage” value to the damage that you want your weapon to deal. (We used 15.0 in this case)

Setting the base damage to 15.0 of the Apply Damage node

Lastly, create a Self node and connect it to the “Damage Causer” pin.

Creating a reference to self

After all these steps, the line trace connections should look like this:

The last step to complete the Line Trace for Objects node and apply damage node.

And here is the complete Net Shoot Weapon event blueprint nodes:

The complete server side system to apply damage to the target actor

Receiving the Damage

Firstly create an AnyDamage event in the actor that you want to receive damage.

Creating the AnyDamage event

Create a new variable named “Health” with the type of float. This will be used to store the amount of health the actor has.

Creating the health variable

Set the default value of the “Health” variable to 100 (or whichever you want your actors max health to be)

Setting the default value of the health variable to 100

Next create a Get node for Health, subtract Damage from Health and then set Health to this new value.

Calculating the remaining health after dealing the damage

From the Set Health node create a “Less than or Equal” node, create a branch node and connect them together. This checks if the player’s health is less than or equal to zero after taking the damage

Checking if the players new health is less than or equal to zero
Checking if the actor has no health remaining

Finally, on the True execution pin create a “SetActorLocation” node and set the New Location value to where you want your actors to respawn when they run out of health.

If the actor's health is less than or equal to zero

Here is the complete Event AnyDamage blueprint nodes:

The complete Event AnyDamage blueprint nodes

Demonstration

The multiplayer FPS health and damage system working in engine.
Players being hit with damage and respawning when running out of health.

To visualise the health of the hit player easier we added a “Print String” node here:

Easily debugging player health by adding a print string node

Download the Project Files

Conclusion

Now your game or project has a fully multiplayer compatible damage system all using blueprints!

Expanding this System

To improve this system you could add a widget to the screen showing when a player is defeated and with what weapon they were defeated by.

Another way to improve this shooter style game would be to add attenuation to the shooting sound effects.

This would reduce the volume over distance and make the game more realistic. Click here to check out our guide on attenuation in Unreal Engine 4.

Lastly, try expanding from this guide by creating AI character with health and damage with our easy AI movement guide. https://couchlearn.com/easy-ai-movement-in-unreal-engine-4/

Further Reading

Unreal Engine’s blog post on their damage system: https://www.unrealengine.com/en-US/blog/damage-in-ue4#:~:text=Damage%20support%20is%20a%20feature,your%20damage%20model%20when%20needed.

The full damage blueprint node documentation can be found here: https://docs.unrealengine.com/en-US/BlueprintAPI/Game/Damage/index.html

Be the first to comment

Leave a Reply

Your email address will not be published.


*