Unreal Engine. Character Interaction With an Object and Animation

Let’s find out how we can implement a simple character interaction with a door mesh with some simple animation using Unreal engine 4.
Before we will start our exploration let’s take a look at what we will be creating (gif)

What will be covered in this story:
- Creating a Door Blueprint
- Adding Interact interface
- Adding an interface to the Door Blueprint
- Implementing event
- Adding a collision to the Door mesh
- Creating custom character and game mode
- Adding interaction button
- Implementing interaction
- Adding animation to the door
We will start from a simple Unreal 3D-person project with starter content (don’t forget to include it at the beginning):

Next, we will remove all unnecessary objects from a scene:

Now we are ready to implement new features!🎉
Creating a Door Blueprint
We will use a simple door from a starter content that will consist of a SM_Door
and SM_DoorFrame
:

Now we will need to combine those into one object with the use of a Blueprint Class
, let’s create one

Pick an Actor Class
from the menu:

You should see something like this:

Open that Blueprint in the editor by double-click on a DB_Door
icon:

Click on Add Component and add a Scene
from a dropdown menu:

This is how the result should look like:

Now we will need to select our Static meshes and “attach” them to the components. We will start from a SM_Door
:

And now select a SM_DoorFrame
:

What should we get in the result:


Now, change the Mobility
of the DoorRoot to Static
:

Add Box Collision
component into the DoorRoot (gif)

… and adjust collision props (approximately like in the picture).
This “area” will trigger an action when our character will collide with it.

Save your work and return to the main project window.
Adding Interact interface
Before we will add some “interaction” we will do it in a proper way at the beginning (not simple solutions, but proper ones 😉). In simple terms: if our “object” is interactable (implements the Interactable interface) — do something. Let’s begin…
Add a Blueprint Interface
:

I will name it BPi_Interactable
:

Open interface file in an editor:

Change Inputs → Interactor type to Actor
:

And rename NewFunction_0
to Interaction
and don’t forget to Compile:


Adding an interface to the Door Blueprint
Now we will need to open BP_Door
file and click on the Class Settings. In the Interfaces tab add our newly created Interface BPi_Interactable
:

You will see that Interfaces have updated:

Next, we will need to Implement an event. Right-click on an Interfaces → Interaction icon and click on Implement event
:

Implementing event
After clicking Implement event
- Event Graph will be opened:

In the Variables tab click on “+ Variable” and add a new boolean variable isClosed
:

… and make it editable:

Now you can just drag n drop it into the Event Graph window:

We will need a reference to our door, so just drag and drop the “Door” static mesh to the Event Graph:

Create a “link” from our Door reference to the SetRelativeRotation
that will allow our door to “open”:

Our final result should look like this:


Adding a collision to the Door mesh
Before we will use the door from the starter content we will need to add collisions. Without it, our character will go through that door.
To know more about Collisions you can check here:
Doble click on SM_Door
:


This guide is not about collisions, so we will use a simple solution. Click on Collision → Add 26DOP Simplified Collision:

After that action, you will see a new collision grid on a door that will be some sort of a barrier:

Creating custom character and game mode
We will need to modify our character and a game mode. We will make an extension of existing functionality, not updating an existing default implementation:

In your /Blueprints
folder create a new Child Blueprint Class
from a ThirdPersonCharacter
and ThirdPersonGameMode
(right-click on them and select the Crete Child Blueprint Class
menu item):


Open CustomGameMode
and change Default Pawn Class value from ThirdPersonCharacter
to CustomCharacter
(don’t forget to compile after):

Open project settings…

… and change Default GameMode
and Default Pawn Class
to our Custom mode and character:

Next, in the World Outliner
change the character of the level. You can just delete the old one and place a new one:

Change Auto Possess Player to Player 0:

Adding interaction button
For our “interaction” we will need to specify an “interaction” button, let’s do that. Open Project Settings
:

And add F
key as our interaction button:



Implementing interaction
Double-click on a CustomCharacter
Blueprint and add Interact
action, it will be our starting point:

Our final code should look like this:

Where is Capsule
came from? No worries, just drag and drop CapsuleComponent
from the current CustomCharacter
:

Adding animation to the door
For simplicity, I have moved our Blueprints to the /Blueprint folder. It is not required, so no worries 😉

Open BP_Door
in the editor and add the Timeline
node:


Open it and click Add Float Track
icon:

Add a key point…

…with values 0 and 0

And second key point with values 1 and 1:

Select those two points and right-click and select an Auto
Key Interpolation, it will make our animation more “natural”:

Change Length to 1:

Our result Blueprint after adding key point should look like this:

NOTE: We need to multiply 1 to the angle value so that our animation would go from zero to one.
Final result
