skip to Main Content

Creating a Cover System

In this post we’re going to create a simple cover system in the Third Person C++ Project template.

Before we start, here is the end result of this post:

In this post I’m using the 4.13 version of the editor so in case you’re using a different version you may need to adjust your code a bit. Moreover, this post requires some assets which I’ve obtained through mixamo.

Before we move on, let’s take a step back to explain how our final system will work.

  1. First of all, we’re going to create a Crouch mechanic for our character (this step requires some code and animation integration).
  2. Then, we’re going to create a cover actor class. In this class, we’re going to add a static mesh (this will be the cover for our character) and a box component which is used to check if the player is near a cover.
  3. If the player is near a cover (meaning, inside the mentioned box component) he can toggle his cover status which will result in a different movement system. Specifically,
    1. If the character is in cover, he will only be able to move along the cover. This means that we’re going to write some code to adjust he’s movement direction and rotation.
    2. If he’s not in cover, we will maintain the default movement functionality and rotation.

System Requirements and Asset Preparation

For this system you’re going to need the following animations:

  • Crouch idle / walk animation
  • One (or two – I will explain later why) Idle animations, Right and left cover movement (in place animations)

These animations, can be obtained through mixamo. When you have the required animations, you need to retarget them, in order to be able to use them inside in the character that comes with the Third Person Tempalte project. I won’t explain how to retarget animations since Unreal Engine documentation provides a detailed workflow which is perfect for our needs.

The following sections assume you have successfully retargeted the mentioned animations and you’re familiar with the persona editor.

Setting up a Crouch functionality for the Character

Before we start typing our code, add the following key bindings:

key_binds

Then, open up the header file of your character and add the following code:

When you’re done with that, inside the constructor of your character, add the following line of code:

and inside the SetupPlayerInputComponent, add the following code:

Then, provide the following implementation for the ToggleCrouch function:

Please note that we’re going to add some functionality in the above function later on. Moreover, provide an empty implementation for the ToggleCover function.

Save and compile your code. Select the character blueprint that is placed in your map and uncheck the Hidden In Game property of the capsule component:

capsule_hidden_ingame
Click on image to enlarge in a new tab

At this point, if you play the game and activate the crouch functionality you won’t see any animation changes, however you will see that the capsule component shrinks (this is why we’ve disabled the hidden in game property – so we can test our functionality first).

Let’s integrate our animations then!

Setting up the Crouch animations for the Character

Open the event graph of the Animation Blueprint that comes with the Third Person C++ Project template and add a boolean variable, named IsCrouching.

Then, add the following logic:

animbp_croucheventgraph
Click on image to enlarge in a new tab

In case you noticed that I haven’t attached the whole logic of the animation it’s because the rest nodes will be explained in a different part in this tutorial.

When you’re done with the logic above, inside the default state machine of the Blueprint, add the following code:

animbp_crouchidle

Here is the transition logic between Idle/Run and Idle/Walk_Crouch:

crouchtostandingupdated
standing_to_crouch

Inside the Idle/Walk_Crouch state, we’re going to play a Blendspace (1D) which will contain the idle and walking animations while crouching.

Create an 1D Blendspace named BS_Crouch and add the following logic:

bs_crouch

Then, inside the Idle/Walk_Crouch state, play the blendspace we created in the previous step. In the parameter of the blendspace, pass the “Speed” parameter which is set in the generated blueprint code inside the Animation Graph:

crouchstate

Save and compile your Blueprints. At this point, you will have a fully functional crouch system.

Understanding the logic behind the Cover Actor class

At this point, we’re almost ready to create the Cover Actor class. Besides the mentioned functionality, this class will contain some functions that will help us decide the right movement direction as well as the right rotation for our player, in case he decides to take cover to a particular actor. Before we dive into code, let’s explain this logic.

The static mesh of the CoverActor will contain four sockets, one in each facing directions. Depending on the side of the Mesh that the player takes cover, his movement direction changes. To better understand this, open up the 1M_Cube_Chamfer mesh (it can be found inside the Geometry -> Meshes folder in the project template) and add the following sockets:

  • A socket named ForwardSocket, with a relative location of: 51 , 0 , 0
  • A socket named BackwardSocket, with a relative location of: -51 , 0 , 0
  • A socket named RightSocket, with a relative location of: 0 , 51 , 0
  • A socket named LeftSocket, with a relative location of: 0 , -51 , 0

In the mentioned locations, the first, second and third values correspond to X, Y and Z axis respectively.

The reason we’ve added a value of 51 to all sockets, is because the mesh if 100 x 100 x 100 units and we want the sockets to be out of the mesh but not too far away from it (I’m going to explain later why is that).

Here is a screenshot of what you’re mesh will look like after adding the mentioned sockets:

socketedmesh
Click on image to enlarge in a new tab

Hopefully, you have noticed that I’ve activated the Pivot Point for the mesh in the previous screenshot. This is going to help me explain how we’re going to decide the movement direction for our player.

Imagine that the player is taking cover on the side of the ForwardSocket. By doing so, the player will assume that by pressing the MoveRight keybind, his character will move opposite to the Green Vector of the mesh (which is the Right Vector of the mesh, meaning a vector with the following properties: [0 , 1 , 0]). Ultimately, the movement direction of the character, equals to: [ 0 , -1 , 0 ]. Moreover, when the player is taking cover on the side of the BackwardSocket, we want to move him along the Green Vector, meaning that the movement direction equals to [0 , 1, 0].

That explains the movement direction when the player is either on the front or back side of the mesh. Using the mentioned workflow, the way that the movement direction is decided when the player is taking cover in the left or right side of the mesh is the same.

Imagine that the player is taking cover on the side of RightSocket. By doing so, the player will assume that by pressing the MoveRight keybind, his character will move along the Red Vector of the mesh (which is the Forward Vector of the mesh, meaning a vector with the following properties: [1 , 0 , 0]). In case the player is taking cover in the LeftSocket, the movement direction will be the opposite, meaning [ -1 , 0 , 0].

The previous paragraphs, explain the code we’re going to implement in the CoverActor class.

Having said that, let’s get started!

Creating the Cover Actor class

Create a C++ class, named CoverActor that inherits the actor class, and add declare the following functions:

Then, inside your source file make sure to declare the character’s header file, in my case this is the “CoverSystemCharacter.h” file and add the following implementations:

Setting up our Character’s logic

Open up the character’s header file, include the header file of the CoverActor class right before the .generated.h file and add the following declarations:

Then. implement the following logic for the SetCanTakeCover function:

Moreover, the time has come, to add an implementation for the ToggleCover function:

Once we’re done with that, let’s restrict the player’s movement when he’s taking cover. Specifically, we don’t want to calculate inputs from the MoveForward function since we want to move right or left only. Here are the edited implementations of the MoveForward and MoveRight functions:

The last step, is to actually inform the character that he’s able to take cover. To do that, provide the following logic in the OnCompBeginOverlap and OnCompEndOverlap functions inside the cover actor class:

Save and compile your code. Then, create a Blueprint based on the CoverActor class and assign the socketed mesh to your SM. Then, extent the BoxComponent so the player can overlap when he’s near the mesh:

boxextent
Click on image to enlarge in a new tab

Then, place some Blueprints in your map and test the functionality. Your cover system will work fine, except for the animations!

Creating the Cover Animations for our Character

In order to create the cover animations for our character, add the following properties in its header file:

Then, modify the MoveRight function:

Save and compile your code. Then, create a Blendspace and assign the following values:

gridblendspace_updated

The reason I’m using a 2D Blendspace instead of 1D is because I want the character to face the direction he was facing when he stopped moving. In case you think that this is unnecessary trouble, you can get away by using a simple 1D blendspace.

Here is each animation with its coordinates:

  • Left cover Idle at 0 , 0
  • Right cover idle at 0 , -1
  • Right cover movement at 1 , 1
  • Left cover movement at -1 , -1

Once you’re done with that, open the Anim Graph of the character’s blueprint and create the following logic:

animbp_graphcomplete
Click on image to enlarge in a new tab

Then, take the time to scroll up and create the logic we skipped in the state machine. The transition rules from cover to crouch and crouch to cover are the following:

covertocrouchupdated
crouchtocoverupdated

Here is the implementation of the CoverState_Idle/Move state:

coverstate

Save and compile your animation Blueprint. Then, have fun with the cover system you’ve just created!

Avatar photo

This Post Has 8 Comments

    1. Blueprints are based on C++ so everything is possible on the latter one. I would choose either of them depending on my project’s needs.

      -Orfeas

      1. I’ll stick with blueprints for that then 🙂

        Do you take requests? I’d love to see an animation tutorial about how to make the third person character be able to walk backwards etc.

        Would be awesome to see one on how its done!

        Thanks

  1. I have tried but the blueprints atleast in my projects are in no way possible to do like yours. Some of the “boxes” didn’t even appear to exist anymore.

    Would it be possible to have access to your project?

    1. Try to recreate your problem in an empty project. I think you’re missing a step in the process (not sure which though).

      -Orfeas

  2. In the animation graph blueprint where are you getting ‘previous direction’ variable? Or is that just a variable you created? Can’t seem to get the cover system to work

  3. Updates for this tutorial

    1. Your capsule will not update when crouching after your first prompt to recompile and setting is enabled.

    Select the character -> in the details tab select CharacterMovement(Inherited) -> Scroll down to NavMovement -> In MovementCapabilities enable Can Crouch

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top