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?
Setting up the Tunnel Blueprint
BP_Tunnel will be responsible for two things. The first is detecting when the game should spawn a new tunnel. To do that, you will create a trigger zone. Once triggered, BP_Tunnel will tell BP_TunnelSpawner to spawn a new tunnel. By doing this, you can create the illusion of an endless tunnel.
The second thing it will do is define a spawn point. BP_TunnelSpawner will then use this point as the next spawn location.
Let’s start with creating the trigger zone.
Creating the Trigger Zone
Open BP_Tunnel and then go to the Components panel. Add a Box Collision component and name it TriggerZone.
The collision area is quite small at the moment. Go to the Details panel and locate the Shape section. Set the Box Extent property to (32, 500, 500).
Next, set the Location property to (2532, 0, 0). This will place TriggerZone right at the end of the tunnel mesh. This means a new tunnel should only spawn when the player reaches the end of a tunnel.
Now, it’s time to create the spawn point
Creating the Spawn Point
To define the location of the spawn point, you can use a Scene component. These components are perfect for defining locations because they only contain a Transform. They are also visible in the Viewport so you can see where your spawn point is.
Go to the Components panel and make sure you don’t have anything selected. Add a Scene component and rename it to SpawnPoint.
The tunnel mesh is 2500 units long on the X-axis so that’s where the attach point should be. Go to the Details panel and set the Location property to (2500, 0, 0).
The next thing to do is to create a function that spawns a tunnel at SpawnPoint.
Spawning Tunnels at the Spawn Point
Click Compile and then switch to BP_TunnelSpawner.
The next BP_Tunnel should spawn at the SpawnPoint of the furthest tunnel. By doing this, the tunnel will always continue.
Since the furthest tunnel is always the last spawned tunnel, you can easily get a reference to it.
Open the graph for SpawnTunnel. Right-click the Return Value pin of the Spawn Actor From Class node. Select Promote to Variable and rename the variable to NewestTunnel.
Now, you will always have a reference to the furthest tunnel.
Next, create a new function and name it SpawnTunnelAtSpawnPoint. Create the following graph:
This setup will get the newest tunnel and the location of its SpawnPoint component. It will then spawn a new tunnel at this location.
In order for BP_Tunnel to communicate with BP_TunnelSpawner, it needs a reference. Without communication, BP_TunnelSpawner won’t know when to spawn the next tunnel.
Creating a Reference to the Tunnel Spawner
Click Compile and then close the SpawnTunnelAtSpawnPoint graph. Afterwards, switch to BP_Tunnel.
Add a new variable and name it TunnelSpawner. Set its Variable Type to BP_TunnelSpawner\Object Reference.
Click Compile and then switch back to BP_TunnelSpawner.
Open the graph for SpawnTunnel and add the indicated nodes:
Now, every tunnel will have a reference to BP_TunnelSpawner.
Next, you will tell BP_TunnelSpawner to spawn the next tunnel when the player enters TriggerZone.
Scripting the Trigger Zone
Click Compile and then switch to BP_Tunnel.
Go to the Components panel and right-click on TriggerZone. Select Add Event\Add OnComponentBeginOverlap. This will add the following node to your Event Graph:
This node will execute whenever another Actor overlaps TriggerZone.
First, you should check if the Actor that overlapped TriggerZone is the player.
Left-click and drag the Other Actor pin. Release left-click on an empty area and select Cast to BP_Player from the menu.
Next, add the indicated nodes after the Cast to BP_Player node:
Let’s go through this step-by-step:
- When an Actor overlaps the TriggerZone, the On Component Begin Overlap (TriggerZone) node will execute
- The Cast to BP_Player node checks if the overlapping Actor is the player
- If it is the player, then BP_TunnelSpawner will spawn a new tunnel. Its location will be at the SpawnPoint component of the last spawned tunnel.
- Since there is no more use for the old tunnel, the game removes it using the DestroyActor node
Click Compile, go back to the main editor and then press Play. Once you reach the end of a tunnel, the game will spawn a new one.
Although the game is endlessly spawning tunnels, it doesn’t look endless. You can mitigate this by always having a few tunnels visible. Later on, when you combine this with obstacles, the player won’t be able to see the tunnels spawning in.
Spawning More Tunnels
The first thing to do is create a function that spawns a certain number of tunnels.
Open BP_TunnelSpawner and create a new function called SpawnInitialTunnels.
To spawn a specified number of tunnels, you can use a ForLoop node. This node will execute connected nodes a specified amount of times. Add a ForLoop node and connect it to the Entry node.
To make the ForLoop node execute n amount of times, you need to set Last Index to n – 1.
For this tutorial, you will spawn three tunnels. To perform three loops, set the Last Index value to 2.
When the game starts, the player should always start in a tunnel. To do this, you can spawn the first tunnel at the player’s location.
Spawning the First Tunnel
To determine if the first tunnel has spawned, you can check if NewestTunnel is set. If it is not set, that means the first tunnel has not spawned. This is because NewestTunnel is only set after the game spawns a tunnel.
To perform this check, add an IsValid node (the one with a question mark icon) after the ForLoop node.
Next, get a reference to NewestTunnel and connect it to the Input Object pin of the IsValid node.
If NewestTunnel is not set, the Is Not Valid pin will execute and vice versa.
Add the following and connect it to the Is Not Valid pin of the IsValid node:
This setup will spawn a tunnel at the location of the player Pawn.
Next, you will spawn the subsequent tunnels.