Journal 78 — Unity 2.5D Platformer, Build Puzzles

Chris Nielsen
6 min readAug 29, 2021

--

Objective: Use an Environment Asset pack to build platformer puzzles

In the previous articles, we started setting up a 2.5D Platformer with a player that can jump, we added collectables, and movable platforms. In this article, we have added some basic puzzle assets that we want to build out: make a working elevator, add wall jumping skill, and add a push object and pressure plate effect.

Working Elevator

As you can see from the scene layout, we have our basic level set up and there’s a panel with a light to call the elevator. We want to add functionality to press the ‘e’ button and change the light color and raise and lower the elevator. We can start with a new C# script for the elevator panel.

Let’s get the first part of the script working to turn the light green at the panel.

To implement this, we can check the player is triggered and staying at the elevator panel and set a bool to true. Then in the Update method, we can check if the ‘e’ key is pressed, and change the material color.

Required Coins

Let’s add an additional criteria to collect all coins before continuing. Since we are tracking the coins picked up in the Player script, we can add a public int method to return the number of coins. Then in the elevator panel script, we can add a check that the collected coins is greater than or equal to the required coins in order to change the light color.

Call the Elevator

Let’s add the ability to call the elevator. We can add a reference to the Elevator game object in the elevator panel. In a new Elevator script, we can create a public method to call the elevator and use FixedUpdate to move the elevator from its origin to a target. We can create the origin and target by duplicating the elevator object then turning off the renderer and colliders and deleting the child game objects.

Let’s first get the elevator working to move down to the target when the ‘e’ key is pressed.

Now let’s add another function to press the ‘e’ key again and move the elevator back up to its origin. We can start by increasing the collider size so it covers the elevator panel and the elevator.

In the ElevatorPanel script, we can add a bool to check if the elevator was called, and have two conditions for pressing the ‘e’ key and changing the light from red to green to red.

Note there is a jitter effect while moving on the elevator. This is because we are not parented to the elevator. This is a similar issue we addressed when on the moving platforms. Let’s add a collider on the elevator to check onTriggerEnter and onTriggerExit.

Here is the updated elevator movement.

Wall Jumping

Let’s add a new skill to wall jump. The concept is basically when we impact the wall on a jump, we can use this location to then jump opposite of the impact point. To start, we can use a function that’s part of the Character Controller called OnControllerColliderHit, and we can draw a ray at the point normal to the surface we collide with.

Then we can add a condition to only allow wall jumping if the player is not grounded, and has touched the two vertical walls, which we can add a tag to.

Here are the results showing successful wall jumping.

Push the Box

The last puzzle is to allow the player to push a box onto a pressure plate. We can use the same function already used, OnControllerColliderHit, to detect a collision with the movable box, and create variables to move the box along the x-axis with a specified force.

We also need to make sure the box is constrained on the rotation axes, otherwise the box can flip over and over instead of sliding along the floor.

Here are the results.

Pressure Pad

Now let’s get the pressure pad working. We want to push the box until the box is nearly completely on the pressure pad. We can create a script on the pressure pad and use the pressure pad’s collider component to check the position of the box. When the box is close to the center, such as less than 0.05 units, we can change it’s RigidBody to kinematic so it will no longer be affected by forces.

Finally, here are the results of the last puzzle.

Thank you for your time!

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet