In this tutorial, you’ll create a platforming puzzle by building two common gameplay objects:
A pressure-plate switch.
A physics cube that reacts to the player.
You’ll use level design and game mechanics such as time, physics, and damage to adjust the puzzle’s difficulty and introduce consequences for player actions.
Before You Begin
This tutorial assumes you already have an understanding of the following topics, which are covered in the previous sections of the Design a Puzzle Adventure:
Materials
Blueprints and variables
Blueprint Interfaces
You’ll need the following assets created in Create a Key.
M_BasicColor_Blue
An Approach to Gameplay Design
Regardless of genre, compelling gameplay makes players feel something; the excitement of player-versus-player or the comfort of a farming simulator. That emotional response contributes to player engagement. If a game isn’t engaging, players are less likely to continue playing — which can impact your project’s success.
As a game designer, it’s your job to engineer gameplay through level design and game mechanics. You can design a level intuitively but, for the purpose of this tutorial, let’s use formulas to visualize the process.
Level Design
Through level design, you can increase or decrease the difficulty of a task. For example, in a platformer, jumping onto a platform is a task. If you place a platform at the upper limit of a character’s jump distance, you’re increasing how difficult it is to reach the platform and complete the task.
However, if there are no consequences for failing, even a high-difficulty task will likely make the player feel safe.
A feeling of safety is useful in tutorial sections of gameplay, where the player can learn controls without consequences.
However, a constant feeling of safety may not be useful for your game. For example, without tension, a boss fight might feel boring, resulting in a lack of player engagement.
To make the player feel tense, you could introduce consequences by using the game mechanic damage.
Game Mechanics
Damage numerically represents the player’s chances of losing something valuable, such as:
Equipment
Powerups
Level progress
A playable character or non-playable companion.
The difficulty of the task combined with the severity of the consequences can impact how much tension the player feels:
Unnecessarily difficult tasks with low consequences or payoff can cause player frustration.
The amount of tension when performing a task can impact the amount of satisfaction the player feels when they meet a win condition. Satisfaction, especially when combined with a reward, creates motivation to continue playing.
A win condition is a condition that the player must meet to successfully complete a task.
Create a Switch
A switch is a gameplay object that produces an effect when something interacts with it.
By learning how to create a switch, you can design gameplay functionality such as pressing a button to open a door. In this case, you’ll use the switch to change color on collisions with in-game actors.
Build with Logic
kkYour switch will activate when an object or player overlaps it. Before you begin building, let’s draft the logic that powers this interaction by answering the questions: what needs to happen to who — and when?
Here’s a breakdown of the switch’s logic:
At runtime, the switch is off.
If a player steps on the switch, then the switch turns on.
If a player steps off the switch, then the switch turns off.
Since the switch has two states (on and off), you can use materials to visualize each state at runtime. During testing, this will quickly verify that the logic is working correctly.
Create Materials
For the switch’s off state, you'll use M_BasicColor_Blue and for the on state you’ll create an emissive material.
An emissive material emits light.
To create the emissive material, follow these steps:
In the Content Browser, at AdventureGame > Designer > Materials, right-click and select Material.
Name the new material
M_EmissiveColorand double-click it to open the Material Editor.In the Material Graph, right-click and search for Vector Parameter. Click it to populate it to the graph and name it
Color. This controls the color of the material’s light.On the Color node, click the color swatch to open the Color Picker.
Choose your own color or set it to Hex sRGB
27F774FFto follow along.Right-click and search for two more nodes: a constant and a multiply node.
Connect the Color node to the A pin of the Multiply node. Then, connect the Constant to the B pin of the Multiply node. Finally, connect the Multiply node to the Emissive Color pin of the M_EmissiveColor node.
The Value of the constant controls the material’s emissive power or brightness. Adjust it as preferred or set it to
25to follow along.Save and close the material.
Your Materials folder should now look like this:
Because M_EmissiveColor is the only emissive material in this tutorial, you don’t need to create any instances. However, material instances are an elegant way of keeping your project modular and working efficiently in a development environment.
Next, you’ll build the switch.
Set Up a Blueprint Class
The switch will be a Blueprint that consists of a static mesh and a box collision. The box collision will detect contact with the player, which triggers the switch’s on and off states.
Collision is the detection of two objects coming into contact at runtime. Box collisions, along with sphere and capsule collisions, are volumes used to make these detections. For example, hitboxes are an example of using collision shapes to detect a successful attack.
To begin, follow these steps:
In the Content Browser, at AdventureGame > Designer > Blueprints, create a new folder called
Activation.Inside Activation, right-click to create a new Blueprint Class.
In the Pick Parent Class dialog, select Actor.
Name the new Blueprint
BP_Switchand double-click it to openBP_Switchin the Blueprint Editor.In the Components tab, create a static mesh by clicking Add, search for
cube, and select it.Name the cube
Switch.In the Details panel, under Transform, adjust Switch’s scale to
2.0,2.0,0.1.In the Components tab, with Switch selected, create a box collision by clicking Add and search for
box collision.Name it
Trigger.In the Details panel, under Location, adjust the box collision’s Z value to
200.Under Scale, adjust its scale to
1.5,1.5,5.0. This box collision is thicker than the static mesh to easily capture collisions.Save and Compile the Blueprint.
Your Blueprints folder should now look like this:
To avoid losing work if compiling fails, Save and then Compile a Blueprint after any significant change.
Next, you’ll create variables to control the switch’s behavior.
Create Variables
Through variables, BP_Switch can reference the material that you just created. Instead of hardcoding a particular material into the switch, you can swap out materials on the fly — which you’ll do later in this tutorial.
To create the on and off materials, follow these steps:
In the MyBlueprint tab, under Variables, create two new variables by clicking the + button twice.
Name one
OnMaterialand the otherOffMaterial.Set their pin type to Material Interface (Object Reference).
A Material Interface variable can reference a Material or a Material Instance.
Select OnMaterial. In the Details panel, next to Category, create a new category called
Setup.Click Compile to get access to its Default Value. Under Default Value, select
M_EmissiveColor. This variable is done for now.Select OffMaterial. In the Details panel, add it to the Setup category.
Under Default Value, select
M_BasicColor_Blue.Save and Compile.
Now that the Blueprint can reference the materials, you’ll use logic to instruct the switch when to use them.
Implement Your Logic
You should have a box collision, a static mesh, and two materials. Now the switch’s logic can be written as:
At runtime, Switch should display OffMaterial.
If Trigger detects a collision, then set Switch’s material to OnMaterial.
If Trigger stops detecting a collision, then set Switch’s material to OffMaterial.
To build this logic, you’ll use a construction script to instruct BP_Switch to use OffMaterial at runtime:
In the Construction Script tab, drag off the Exec pin on the Construction Script node and search for Set Material (Switch). Click to create it.
On the Set Material node, drag off the Material pin and search for Get OffMaterial. Select it.
Save and Compile.
You’ll handle the remaining logic in the EventGraph. To instruct the switch to turn on and off upon overlap, follow the steps below:
In the EventGraph, delete the Event BeginPlay, Event ActorBeginOverlap, and Event Tick nodes. You won't need them.
In the My Blueprint tab, under the Components heading, right-click on Trigger and select Add Event > Add OnComponentBeginOverlap.
Repeat this process to add an OnComponentEndOverlap node as well.
From the Exec pin of the OnComponentBeginOverlap(Trigger) node, drag and search for Set Material (Switch).
From the Material pin of the Set Material node, drag and search for Get OnMaterial.
From the Exec pin of the OnComponentEndOverlap(Trigger) node, drag and search for Set Material (Switch).
From the Material pin of the Set Material node, drag and search for Get OffMaterial.
Your switch is done for now, Save and Compile.
Your Construction Script graph should now look like this:
Your EventGraph should now look like this:
Now you can test your project to see if your switch functions correctly.
Drag an instance of BP_Switch from the Content Browser into Room 1. Click the three-dot menu in the Playmode toolbar, and select Current Camera Location. This ensures you enter PIE mode from your current location in the viewport, instead of running through the entire level to test one feature.
In PIE mode, when you step onto the switch, it should light up. When you step off, it should return to blue.
The Benefit of Modular Development
Let’s say you want two switches instead of one, but you want the second switch to light up red instead of green. You could build a new switch with the same logic and assign a red material, but that would cost you development time and create more overhead for your project.
Overhead refers to the expenditure of resources (processing power, time, storage amounts, and so on).
Since platforms (computers and consoles) have finite processing power, game developers usually want to work modularly to reduce overhead.
Because you’ve used Blueprints and variables to build your switch, you’re already working modularly. Let’s see your work in action:
Open
BP_Switchin the Blueprint Editor by double-clicking it in the Content Browser.Under Variables, select OnMaterial and click the eye icon so that it’s open.
Select OffMaterial and we’ll do the same thing in a different way; in the Details panel enable Instance Editable. You’ll notice that the eye icon opens; it’s now a public and editable variable.
Save, Compile, and close it.
You should already have one instance of BP_Switch in the level, so drag in a second instance. With either switch selected in the viewport, the Details panel has a new UI category called Setup. Setup displays your public and editable variables as parameters that you can change on the fly from the viewport.
Try changing the materials to anything you want and testing how the switches react in PIE mode. You’ll notice that each instance of your switch can hold unique On and Off materials without creating new switches from scratch — speeding up your development and reducing overhead.
If you want your viewport to look exactly like ours, delete the second instance of your switch before you proceed with this tutorial.
Create Single and Multiple Activations
Your switch currently turns on and off indefinitely. In certain cases, it’s valuable to only activate it once. For example, in a hallway that leads to loot, you might want a switch to activate a trap that the player must avoid. If the player successfully avoids the trap and collects the loot, the switch should not activate the trap again.
Rather than discard what you’ve built already, you’ll add a boolean to permit only one use of your switch.
To do this, follow these steps:
In the Blueprint Editor for
BP_Switch, under Variables, click the + button to add a new variable.Name it
ActivateOnceand set the pin type to Boolean.In the Details panel, check the Instance Editable box.
Click the dropdown menu next to Category and select Setup.
Click Compile to access the variable’s Default Value and verify that it is unchecked. This means that ActivateOnce will not be on by default for this Blueprint.
In the EventGraph, right-click and search for a Branch node.
From the Condition pin on the Branch node, drag out and search for Get ActivateOnce.
Drag the Exec pin of the On Component End Overlap (Trigger) node to the Exec pin of the Branch node. It should disconnect from the SetMaterial node.
Connect the False pin of the Branch node to the Exec pin of the Set Material node. This means that when the object moves off of the switch, a decision is made: if ActivateOnce is True, the switch stops detecting collision. If ActivateOnce is False, the switch works indefinitely.
Save, Compile, and close the Blueprint.
Select
BP_Switchin the viewport. In the Details panel, ActivateOnce should be an toggleable checkbox for this instance. Enable it to see how it works.
Your EventGraph should now look like this:
Click Play to test your variable in game. With ActivateOnce enabled, the switch should stay lit even after walking off of it.
Activate Switches with Physics
Later in this tutorial, your player will need to hold the switch down while they navigate the rest of the level. For this purpose, you’ll create the second gameplay object of this puzzle; the physics cube. This incorporates the game mechanic, physics, into the puzzle you design.
To create a physics cube using Blueprints, follow these steps:
In the Content Browser, navigate to AdventureGame > Designer > Blueprints > Activation, right-click and create a new Blueprint Class.
In the Pick Parent Class dialog, select Actor.
Name the new Blueprint
BP_Cube.Double-click it to open
BP_Cubein the Blueprint Editor.In the Components tab, create a static mesh by clicking Add, search for
cube, and select it.Name the mesh
Cube.In the Details panel, under Static Mesh, search for and select SM_ChamferCube.
Under Transform, set the Location property's Z value to
50.0.Under Materials, set Element 0 to
M_BasicColor_Blue.Under Physics, enable Simulate Physics. This setting enables the use of UE’s Chaos Physics Engine, letting the cube react to the player’s pushing.
Save and Compile.
To test functionality, drag an instance of BP_Cube into your level, near your switch. Press Play to enter PIE mode and use WASD letter keys to push BP_Cube onto a switch to turn it on.
Debug
If you push the cube onto the switch, it turns on, but you might notice a problem. If you walk off the switch, the switch turns off even if the cube is still overlapping it.
Look at the current logic to find the error:
If Trigger stops detecting a collision, then set Switch’s material to OffMaterial.
The switch turns off when Trigger stops detecting any collision, even if there is still one actor overlapping it. To solve this problem, instruct Trigger to check for any overlapping actors before proceeding:
If Trigger stops detecting a collision, and if there are no actors overlapping it, then set Switch’s material to OffMaterial.
To make this adjustment to your Blueprints, follow these steps:
In the Blueprint Editor for
BP_Switch, right-click and create a Branch node.Drag off the Condition pin, search for and create an Is Empty (Array) node.
From the Target Array pin of the Is Empty node, search for and create Get Overlapping Actors (Trigger).
Set the Class Filter to Actor.
Connect this logic to the existing nodes by dragging from the False pin of the first Branch node and connect it to the Exec pin of the second Branch node.
Connect the True pin on the second Branch node to the Exec pin of the Set Material node.
Save and Compile.
Your EventGraph should now look like this:
To adjust Trigger’s collision settings, follow these steps:
In the Components tab, select Trigger.
In the Details panel, expand Collision Presets. Set the Collision Presets dropdown to Custom.
In the Collision Enabled dropdown, select Collision Enabled (Query and Physics).
This setting enables trace detection and physical collision responses on the Trigger box collision, so the box doesn't block anything physically but generates overlap events.
In the Object Type dropdown, select WorldDynamic.
Use WorldDynamic for interactive or moveable actors and gameplay objects.
Next to WorldDynamic, add a checkmark next to Ignore.
Setting WorldDynamic to Ignore here ensures other WorldDynamic objects can't physically collide with the collision box.
Save and Compile.
Now, you've set up the switch's collision box to not block anything and not interfere physically with characters or objects, but detect when something steps into the box and triggers and overlap event.
Play the game to test your solution. The switch should stay lit as long as any actor overlaps it, regardless if one moves off.
Control Other Objects With the Switch
So far, you’ve created a switch that turns on and off. That’s excellent for visualizing if the switch is functioning correctly, but it could be more useful. Next, you’ll use the switch to activate other objects within the level.
First, you’ll create a Blueprint interface with two functions; each one representing the on and off state of the switch. These functions are used like events signaled between objects.
To set up a Blueprint Interface with two functions, follow these steps:
In the Content Browser, navigate to AdventureGame > Designer > Blueprints > Core, right-click, and highlight Blueprint. Then, select Blueprint Interface.
Name the interface
BPI_Interaction. Double-click to open the Blueprint Interface window.In the MyBlueprint panel, a new function should already exist. Name it
fnBPISwitchOn.Click Add, and then function to create a second function. Name it
fnBPISwitchOff.Your interface is complete. Save, Compile, and close it.
Next, you’ll create an array that contains all objects you want the switch to activate. In this tutorial, you’ll use an array to manage which objects your switch activates, rather than creating unique logic for each new object.
To create the array of objects the switch should trigger, follow these steps:
In the My Blueprint tab of
BP_Switch, under Variables, click the + button to create a new variable.Name it
InteractObjectListand set the pin type to Actor (Object Reference).In the Details panel, next to Variable Type, set the container type to Array.
To make it a UI parameter, check Instance Editable and add it to the Setup category.
Save and Compile.
Using logic, you’ll instruct the switch to iterate through each object in the array (which is currently empty) and signal it using the BPI_Interaction interface you created earlier. To do this, you’ll use a For Each Loop.
To signal all items in the array, follow these steps:
In the EventGraph, drag out from the Exec pin of the Set Material node (of the On Component Begin Overlap node), and search for For Each Loop.
To instruct which objects to perform the event for, drag out the Array pin on the For Each Loop and search for Get InteractObjectList.
Finally, drag out the Loop Body pin on the For Each Loop and search for fnBPISwitchOn. This is the event you’re calling.
Connect the Array Element pin on the For Each Loop to the Target pin on the fnBPISwitchOn node.
The same must be done for On Component End Overlap (Trigger). To speed up the process, select the For Each Loop and Interact Object List node and copy it using right-click > Copy or Ctrl + C. Press Ctrl + V to paste it in the graph.
Connect the Exec pin of the Set Material node to the Exec pin of the new For Each Loop.
From the Loop Body pin of the For Each Loop, drag out and search for fnBPISwitchOff. Connect the Array Element pin to the Target pin.
Your switch is done for now. Save, Compile, and close it.
Your EventGraph should now look like this:
You’ve now laid the groundwork for activating a unique list of objects in your level when a player overlaps the switch. In the next tutorial, you’ll create the third gameplay object of this challenge — a moving platform.