How to Create a Simple Game in Unreal Engine 4

In this Unreal Engine 4 tutorial, you will create a first-person endless game. You will learn how to generate random obstacles and restart the game. By Tommy Tran.

4.1 (49) · 1 Review

Save for later
Share

If you’re starting out in game development, one of the common pieces of advice is to create a basic game. This is because it teaches you how to create simple mechanics and how objects interact with each other.

In this tutorial, you will create a first-person game that continues indefinitely. You will learn how to:

  • Move the player forward continuously
  • Generate obstacles the player must avoid
  • Randomize the obstacles to create variations
  • Create a restart button that displays when the player hits an obstacle

At the end, you will have a game that looks like this:

Please note, you will be using Blueprints and UMG in this tutorial. If you need a refresher, check out our Blueprints and UI tutorial.

Note: This tutorial is part of a 10-part tutorial series on Unreal Engine:

Getting Started

Download the starter project and unzip it. Go to the project folder and open InfiniteMatrix.uproject.

Note: If you get a message saying that the project was created with an earlier version of the Unreal editor, that’s OK (the engine is updated frequently). You can either choose the option to open a copy, or the option to convert in place.

Press Play to test out the movement controls. You can move vertically and horizontally by moving your mouse.

The first thing you will do is make the player move forward continuously.

Moving the Player Forward

Navigate to the Blueprints folder and open BP_Player.

To move the player forward, you will add an offset to the player’s location each frame.

First, you need to create a variable to define the speed of the player’s forward movement. Create a Float variable named ForwardSpeed and set its default value to 2000.

Next, make sure you are in the Event Graph and then locate the Event Tick node. Create the following setup:

By multiplying ForwardSpeed with Delta Seconds, you get a frame rate independent result.

Note: If you aren’t familiar with frame rate independence, please read our Blueprints tutorial. We cover it in the Frame Rate Independence section.

Next, you will use this result to move the player along a single axis.

Moving Along a Single Axis

To move the player, create an AddActorWorldOffset node. Set Sweep to true by left-clicking its checkbox.

If you try to connect the Float result to the Delta Location input, Unreal will automatically convert it to a Vector.

However, this will put the Float value into the X, Y and Z components of the Vector. For this game, the forward movement should only be along the X-axis. Luckily, you can split a Vector into three Float components.

Make sure the Delta Location pin of the AddActorWorldOffset node has no connections. Right-click the Delta Location pin and select Split Struct Pin.

Finally, connect everything like so:

Let’s recap:

  1. Every frame, the game will multiply ForwardSpeed and Delta Seconds to get a frame rate independent result
  2. The AddActorWorldOffset will use the result to move the player along the X-axis
  3. Since Sweep is enabled, the player will stop moving forward if anything blocks it

Click Compile and then go back to the main editor. If you press Play, you will move through the tunnel.

Instead of placing tunnels manually, you can create a Blueprint that spawns tunnels automatically.

Creating the Tunnel Spawner

Go to the Content Browser and make sure you are in the Blueprints folder. Create a new Blueprint Class with Actor as the parent class. Name it BP_TunnelSpawner and then open it.

Since the game will be spawning tunnels constantly, it’s a good idea to create a spawning function. Go to the My Blueprint panel and create a new function named SpawnTunnel. The purpose of this function will be to spawn a tunnel at a provided location.

To pass a location to the function, the function needs an input parameter. These will appear as input pins when you call the function.

They will also appear as output pins on the Entry node of the function.

Let’s go ahead and create an input parameter. Make sure you are in the graph for the SpawnTunnel function. Select the Entry node and then go to the Details panel. Click the + sign next to the Inputs section.

Rename the input parameter to SpawnLocation and change its type to Vector.

To spawn a tunnel, add a Spawn Actor From Class node. Click the drop-down located to the right of the Class pin and select BP_Tunnel.

To set the spawn location, right-click the Spawn Transform pin and select Split Struct Pin. Afterwards, link the Spawn Actor From Class node to the Entry node like so:

Now, whenever you call the SpawnTunnel function, it will spawn an instance of BP_Tunnel at the provided location.

Let’s test it out!

Testing the Tunnel Spawner

Switch to the Event Graph and locate the Event BeginPlay node. Add a SpawnTunnel node and connect it to the Event BeginPlay node.

On the SpawnTunnel node, set Spawn Location to (2000, 0, 500).

Now, when the game starts, it will spawn a tunnel up and away from the player. Click Compile and then go back to the main editor.

First, delete BP_Tunnel from the level. Do this by left-clicking on BP_Tunnel in the World Outliner. Afterwards, press the Delete key to remove it from the level.

Next, go to the Content Browser. Left-click and drag BP_TunnelSpawner into the Viewport. This will add an instance of it to the level.

If you press Play, the game will spawn a tunnel above and away from the player.

Once you are done testing, go back to BP_TunnelSpawner. Reset the Spawn Location of the SpawnTunnel node to (0, 0, 0).

Afterwards, click Compile and then go back to the main editor.

In the next section, you will set up functionality for BP_Tunnel.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.