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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Create a Simple Game in Unreal Engine 4
30 mins
- Getting Started
- Moving the Player Forward
- Moving Along a Single Axis
- Creating the Tunnel Spawner
- Testing the Tunnel Spawner
- Setting up the Tunnel Blueprint
- Creating the Trigger Zone
- Creating the Spawn Point
- Spawning Tunnels at the Spawn Point
- Creating a Reference to the Tunnel Spawner
- Scripting the Trigger Zone
- Spawning More Tunnels
- Spawning the First Tunnel
- Spawning Subsequent Tunnels
- Creating Obstacles
- Creating Wall Variations
- Randomizing the Wall
- Handling Wall Collisions
- Setting the IsDead Variable
- Displaying a Restart Button
- Creating the Display Function
- Calling the Display Function
- Restarting the Game
- Resetting the Player
- Respawning the Tunnels
- Handling Button Clicks
- Where to Go From Here?
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.
- Part 1: Getting Started
- Part 2: Blueprints
- Part 3: Materials
- Part 4: UI
- Part 5: How To Create a Simple Game (you are here!)
- Part 6: Animation
- Part 7: Audio
- Part 8: Particle Systems
- Part 9: Artificial Intelligence
- Part 10: How to Create a Simple FPS
- Part 1: Getting Started
- Part 2: Blueprints
- Part 3: Materials
- Part 4: UI
- Part 5: How To Create a Simple Game (you are here!)
- Part 6: Animation
- Part 7: Audio
- Part 8: Particle Systems
- Part 9: Artificial Intelligence
- Part 10: How to Create a Simple FPS
Getting Started
Download the starter project and unzip it. Go to the project folder and open InfiniteMatrix.uproject.
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.
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:
- Every frame, the game will multiply ForwardSpeed and Delta Seconds to get a frame rate independent result
- The AddActorWorldOffset will use the result to move the player along the X-axis
- 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.