Create A Parasite Platformer Game In Unreal Engine Part 4: Creating The First Obstacle And Moving It With Blueprint Code

Table of Contents

Create A Parasite Platformer Game In Unreal Engine Part 4: Creating The First Obstacle And Moving It With Blueprint Code

Reading Time: 16 minutes
Level: Beginner
Version: Unreal Engine 4.26

Help Others Learn Game Development

In part 3 of this tutorial series we learned how to write Blueprint visual code and we made the PlayerCharacter move in the level.
 
In this part of the tutorial we are going to create the moving Obstacle for the level.
 

Creating The Moving Cube Obstacle Blueprint

Inside the Blueprints folder in the Content Browser tab Right Click -> Blueprint Class and set the class to inherit from the Actor class:

Img 1

Give the new Blueprint name BP_MovingObstacle and open it in the Blueprint editor.

Under the Components tab click on the green Add Component button and filter for Static Mesh and add that component to the Blueprint:

Img 2

Rename the Static Mesh component by Right Click -> Rename or clicking on the name of the component once you have selected, or select it and press F2 and give it a name Mesh – Obstacle.

For the mesh object we are going to filter for Shape_Cube and for the material of the mesh use M_Brick_Clay_Old:

Again, don’t forget that every time you make a change in a Blueprint, press Compile and Save otherwise the change you made will not reflect, and take this as the most important rule when it comes to Blueprint visual coding.

Before we attach more components to the BP_MovingObstacle blueprint, I am going ti make the Mesh – Obstacle component the root component by dragging it on the DefaultSceneRoot in the Components tab:

The root component defines the Transform e.g. Location, Rotation, and Scale of the Actor in World space e.g. inside the level.

All other components will be attached to the root component and when we move them around their location e.g. position will be in respect to the root components location.

Now we are going to attach a new component to the MovingObstacle Blueprint so click on the Add Component button and filter for arrow:

Img 3
Now attach another Arrow component to the MovingObstacle by selecting the current one that we added, and then CTRL + C to copy it and CTRL + V to paste it. On MacOS it’s CMD + C and CMD + V.
 
Rename the two Arrow components so that one has a name Start Point and the second one End Point.
 
For the Start Point component set the following values for the Location:
 
  • X = 222
  • Y = 0
  • Z = 0
For the End Point component set the Location values:
 
  • X = -222
  • Y = 0
  • Z = 0
And set the following values for the Rotation of the End Point component:
 
  • X = 0
  • Y = 0
  • Z = 180
This how the MovingObstacle Blueprint looks like now:
 
The red arrows that you see in the Viewport represent the Arrow components. Of course, they are not visible inside the game when we start playing.
 
The purpose why we created these Arrow components is that we are going make the Static Mesh component e.g. the Cube e.g. the Obstacle move between the two Arrows.
 
When the game starts, we will store the position of the two Arrows inside the game, and then make the Obstacle move from one to the other.
 

Blueprint Variables

To make the Obstacle move between the Arrows, we need to create variables that will store the position of the Arrows when the game starts.

We saw examples of variables in the Introduction To Variables lecture, but these are C++ variables that we write in code.

Well, we have the same variable types inside the Blueprints that we can declare and use.

We can declare variables inside the Blueprint by clicking on the + button for the Variable list inside the My Blueprint tab:

Img 5

When you click on the + button a new variable will be created and you will see it under the Variables list.

Usually when a new variable is created, its type is the same as the type of the previous variable that we created.

In our case, since this is the first variable we created for this Blueprint, the type is a bool.

We can see that because of the icon that is on the left of the variable name:

Img 6

You can also hover over that icon and you will see that the current type is boolean:

Img 7

We can also select the variable, and inside the Details tab we will see information about the variable such as its type, name and so on:

Img 8
To change the type of the variable we can either click on the icon to the left of the variable name and from the list select the type of variable that we want:
 
Img 9

Or we can select the variable in the Variables list, and inside the Details tab click on the drop down list under the Variable Type and select the type of variable that we want:

Img 10

Also, when the variable is declared it will have a default name like the one you see for our variable which is NewVar_0.

You can rename the variable the same way as we rename components and you can also rename it by selecting it from the Variables list, and inside the Details tab change the name of the variable inside the Variable Name field:

Img 11


Declaring Variables For The MovingObstacle Blueprint

Now that we know how to declare variables let us declare all the variables that we will need for the MovingObstacle Blueprint:

Img 12

As you can see we have in total 7 variables that we will use to move the Obstacle from Start Arrow to End Arrow.

We have the float type Frame Time variable which is going to represent the delta seconds time between each frame e.g. the time between each frame, and we will use this variable to smooth the movement of the Obstacle.

Next we have the float type Movement Speed which is the variable that will determine how fast the Obstacle can move between two Arrows.

Current Location is a vector type variable that we will use to store the current location of the Obstacle.

The Target Location is also a vector type variable that will represent the location where we need to go e.g. the location where the Obstacle is moving to.

Move To Start Point is a bool type variable that we will use to determine if we should move to the Start Point Arrow or to the End Point Arrow.

The Start Point Location is a vector type variable where we will store the location of the Start Point Arrow inside the level world.

And the End Point Location is a vector type variable which will store the location of the End Point Arrow inside the level world.

Declare all these variables with the same types and names as I have declared them.

Public And Private Blueprint Variables

Same as with C++ variables, we can also make the Blueprint variables public and private.

We can set the Blueprint variable to be public or private by selecting the variable in the Variables list, and inside the Details tab check or uncheck the Private checkbox:

Img 13

Setting the variable to private like this will make it accessible or not accessible by other Blueprints, basically the same way how private and public works in C++.

There is another feature that will allow us to make the variable editable or not editable on the instances of the Blueprint.

What does this mean?

Let’s drag the BP_MovingObstacle from the Blueprints folder inside the level and then select it from the World Outliner.

Inside the Details tab we will see all the normal options we have for an Actor:

But if we go back inside the Blueprint editor for the BP_MovingObstacle and select one of the variables, let’s say Movement Speed variable and we click the Eye icon to the right of the variable or the Instance Editable checkbox inside the Details tab:
 
If we go back inside the level editor and select the BP_MovingObstacle in the World Outliner, we will now see the Movement Speed variable inside the Details tab for the BP_MovingObstacle:
 
Img 16

This way we can now edit the Movement Speed variable value directly on the instance of the BP_MovingObstacle which means for every instance e.g. object that we create from the BP_MovingObstacle inside the level, we can set a different value for the speed.

For the BP_MovingObstacle, Movement Speed and Move To Start Point variables are going to be Instance Editable so make sure that you enable that for both variables using one of the ways that we already explained above:

Img 17

When you are done, press the Compile and Save buttons for those changes to take affect on the BP_MovingObstacle Blueprint.

Setting The Default Values For Blueprint Variables

While we can edit the variable values on the instances of the Blueprint, we can also set a default value for every variable that we create.

The default value will be applicable to every instance of the Blueprint and that is the value that will be used in case that we don’t change the that value by making the variable visible in the Details tab.

To set a value for the variable, inside the Blueprint editor for the BP_MovingObstacle Blueprint, select the variable you want and inside the Details tab you will see Default Value setting and below it you can set the value that you want:

If you don’t set any value for the variable you will see its default value which for a float is 0.0

For the BP_MovingObstacle we are only going to set the default value for the Movement Speed variable, so make sure that you select it and set its value to 200:

Img 19


Setting The Variable Values Inside The Blueprint Editor

Now that we know how to work with variables let us initialize them starting in the BeginPlay event.
 
Inside the BP_MovingObstacle Blueprint editor in the Event Graph, drag the Current Location from the Variables list inside the Graph:
 
When you do that you will be prompted to either create a Getter or a Setter for the Current Location.

We already saw this and we know that the Getter will get a reference to the variable and we can then call functions and variables of that reference or we can use its value.
 
The setter will enable us to set a new value for that reference or variable.
 
In this case we need the Setter because we are going to set the initial value for the Current Location.
 
Connect the execution flow from BeginPlay Exec to the Setter of the Current Location, and then Right Click and in the search filter for Get Actor Location and plug the return value of that function in the Setter of Current Location:
 
Img 21 - Node Code Dont Compress Too Much

You can copy the nodes from here:

Using Components As Variables

The components we attach to an Actor such as Static Mesh, Arrows, and so on, can also be used as variables inside the Blueprint.

If you take a better look under the Variables list, you will see another sub list Components and under that list you will see a list of components that we attached on the MovingObstacle Blueprint:

Img 22

You will also notice that the names of the components are the same as the ones we gave them under the Components tab.

We can get a reference to these components inside the Event Graph by dragging them from the Variables list:

When we drag a component like this we can select if we want the Getter or the Setter for that component.

We can also drag the component directly from the Components tab, but in this case we can only create a Getter for that component and not a Setter:

And these are the ways how we can get references to the components that are attached on the Blueprint.
 
When we have a reference to components we can use all their public variables and functions and manipulate them.
 
Now we can set the initial values for all the location variables:
 

You can copy the nodes from here:

To set the values for the Start Point and End Point Location variables we need to get the world location of the arrows representing the start and end point.
 
For that we use the GetWorldLocation function which will get us the location of the Arrow component inside the world e.g. inside the level of the game.
 
This way we can move the Arrow components around in the level and create two points in spaces, one will be the Start Point Arrow the other one will be the End Point Arrow and when we run the game and the Blueprint code executes we will get their location in the world space and make the Obstacle move between the two points.
 

If Else Statements For Blueprint Code

The Move To Start Point variable is going to control whether we are moving the Obstacle towards the Start Point Location or the End Point Location.

We also made this variable Instance Editable which allows us to edit it on every copy of the BP_MovingObstacle Blueprint.

This way we can create multiple Obstacles and every Obstacle can start the movement in the opposite direction e.g. one Obstacle will move towards the Start Point Location and the other will move to the End Point Location.

To control this, we need an if statement so that we can test if the Move To Start Point bool variable is true or false.

In Blueprint visual coding, the if statement is represented with a Branch, it’s basically the same thing as the if statement except the name is different.

So the next step after we set the value for the End Point Location is to create a branch, and this can be done by Right Click and in the search field type branch:

Img 26
When you create a Branch node you will see that it takes a condition which a bool variable in our case Move To Start Point, and it has to Exec pins, one if the condition is true and the other if the condition is false:
 
Img 27

Take the Move To Start Point variable and plug it in the condition of the Branch.

If the condition is true we are going to set the Target Location to the Start Point Location, and if the condition is false we are going to set the Target Location to the End Point Location:

You can copy the nodes from here:
 

So first we set the values for the Start Point and End Point Locations by getting the Start Point and End Point Arrows and getting their world locations which will get us the locations of those Arrows inside the level.

Next we check if the Obstacle should move to the Start Point, if it’s true, we set the Target Location to the Start Point Location, if it’s false, we set the Target Location to the End Point Location.

We already mentioned this but you can also see from this set up that we will use the Target Location vector variable as the destination when we move the Obstacle Blueprint in the code.

Moving The Obstacle

The movement of the Obstacle is going to happen in the Tick Event.
 
The Tick event is called every frame while the game runs, so if we have 60 frames in 1 second, then the Tick event will be called 60 times.
 
We are going to use a function called VInterp to Constant to create a smooth movement of the Obstacle:
 

You can copy the nodes from here:

The first thing we are doing is we are storing the Delta Seconds in the Frame Time.
 
Delta Seconds is the name of the returning parameter of the Tick event and it represents the time between frames e.g. how much time it takes from one frame to go to the other.
 
This value is used when you want to smooth out the movement or make it frame independent.
 
Because you can imagine if you use a fixed value of let’s say 200 and you move an Actor with that value inside the Tick Event.
 
Since the Tick Event is called every frame if we have 60  frames you can image how fast that Actor is going to move, that’s why we use Delta Seconds to multiply the speed and make the movement smoother.
 
The VInterp to Constant function will move from the Current, to the Target using the Delta Time and Interp Speed to smooth the movement.
 
Then we will set that value to the Current Location and we will use that value and pass it to the SetActorLocation function which is the function that moves an Actor, and this is the code that will move the Obstacle.
 
Before we test this out, make sure that you put the Start Point and End Point Arrows of the BP_MovingObstacle away from the Cube Mesh inside the Level editor:
 

As you can see, we can control the Arrows inside the Level editor by selecting them in the Details tab after we selected the BP_MovingObstacle in the World Outliner.

We can then move the Arrows anywhere in the level and when the game starts, we will store their world location in the Start Point and End Point Location variables in the Blueprint and we will use those values to move the Obstacle from one location to the other.

Before we run the game to test it, next to the Play button in the Editor there is a drop down list, click on it and under the setting where it says Spawn Player At click on the Current Camera Location:

Img 30
This will spawn the PlayerCharacter where the camera is currently looking in the Editor and not where the Player Start Actor is.
 
This is convenient for testing purposes as we can move the camera wherever we want in the level, and then run the game and we will start the game from that position.
 
We can always change those settings back by clicking on Default Player Start in the same settings.
 
Now when we run the game we will see that the Obstacle is moving either towards the Start Point Location or the End Point Location depending on whether the Move To Start Point variable is checked in the Details tab or not:
 

As you can see the Obstacle is moving to the Start Point Location when the Move To Start Point bool variable is checked and it moves to the End Point Location when the Move To Start Point bool is not checked.

We also saw that the Obstacle was moving faster when we changed the Movement Speed to a higher value.

The same way it would move slower if set a lower value for the Movement Speed variable.

The cool part about this method is that you can move the the Start Point and the End Point Arrows anywhere in the level and when the game starts the Obstacle will move between them.

Moving The Obstacle Between Two Points

While the Obstacle is moving it only goes to either the Start Point or the End Point Arrow location and stops after that.
 
We need to make it move between the two points so that when it reaches one point it will start to move towards the other point.
 
To do this, we need to test if we reached the target location. Inside the BP_MovingObstacle Blueprint editor create a new Branch node from the SetActorLocation node.
 
Next, Right Click and search for the GetActorLocation function node, and get a reference to the Target Location:
 
Img 31- 75%
To calculate if we reached the Target Location we are going subtract from the current Actor location which we can get from the GetActorLocation function, the Target Location.
 
To do that, Right Click and search for vector – vector function:
 
Img 32

In the first parameter plug in the return value from the GetActorLocation function, and in the second plug in the Target Location because we are subtracting the Target Location from the Actors current location.

This calculation will return a new vector variable which is the subtraction of the two mentioned vectors above.

From the returning value of the vector – vector function, drag a node and search for vector length:

Img 33

As you can see the VectorLength function returns a float value. That float represents the length of the vector.

We can think of the length of the vector as all 3 axis e.g. X, Y, and Z, combined into one straight line, and the length of that line is the length of the vector.

In our case, the closer the Actor location is to the Target Location, the length of the vector will be smaller.

We can use that to our advantage and compare the returned value of the VectorLength function with 0(zero) and that will be our condition for the Branch.

To compare variables to each other, Right Click and in the search bar type the comparison you want to make e.g. >, <, >=, <=, ==, and you will see functions for comparing different variables like floats and integers:

Img 34
Since the return value of the VectorLength function is a float we are going to compare a float to a float.
 
In the first parameter for the comparison function plug in the returned value from the VectorLength function, and for the second value we are not going plug in anything, instead we are going to leave the value 0.0:
 

You can copy the nodes from here:

As I already mentioned, every functionality that we have in C++ we have in Blueprints as well which we see from the comparison node which allows us to compare two variables the same way we compare them using C++.

If the condition that we set up is true meaning the length of the returning vector when we subtract the Target Location from the Actors current location is less than or equal to 0(zero), we are going to go inside another Branch.

The condition for that Branch is going to be the Move To Start Point bool variable.

The reason for that is when we reach the Target Location we need to change the movement of the Obstacle e.g. if the Target Location was the Start Point Location and we reach it, now we need to make the Obstacle move to the End Point Location which means we need to set the Target Location to be the End Point Location:

You can copy the nodes from here:

If the Move To Start Point is true, we need to set the Target Location to the End Point Location so that now the Obstacle will start moving towards the End Point Location, as we already explained.

If the Move To Start Point is false we need to do the vice-versa e.g. we set the Target Location to the Start Point Location so that the Obstacle continues moving towards the new Target Location.

What is also important is to change the Move To Start Point value when we change the movement.

So when we set the Target Location to the End Point Location, we set the Move To Start Point to false denoting that we are not moving towards the Start Point Location but we are moving towards the End Point Location.

And when we set the Target Location to the Start Point Location we set the Move To Start Point to true.

If we don’t make these changes then again our Obstacle is only going to move towards the Target Location once and then stop moving.

We can now test the game and check out the movement of the Obstacle:


Creating Multiple Obstacles For Our Level

Now that our Obstacle is working we can easily create as many MovingObstacles as we want.
 
First, inside the Word Outliner tab create a new folder under the Level folder and name it Moving Obstacles.
 
Drag the BP_MovingObstacle that we were using as an example inside of that folder and then rename it to Moving Obstacle 1.
 
To create more Obstacles simply duplicate the Moving Obstacle 1 and position it next to the closest obstacle and repeat the process until you are satisfied how many obstacles you have.
 
You can also mix the speed of the Obstacle as well as the movement by checking the Move To Start Point checkbox which will make the Obstacle either move towards the Start Point or the End Point Location:
 


Where To Go From Here

In this tutorial you learned more about Blueprint coding by making the Obstacle move between two points. And we also created the first row of Obstacles for our level.

In the next part of this tutorial series titled Creating The Fire Obstacle Using Inheritance you will learn how to use inheritance to extend the Blueprints we created and create new Actors for our game.

Leave a Comment