My journey becoming a Unity game developer: 2.5D Infinite Runner-Push a box onto a Pressure Pad and Escape

Rhett Haynes
6 min readDec 30, 2021

Objective: Have the Player push a box onto a Pressure Pad, make its way up to the upper area using Moving Platforms, and escape through a portal to the next scene.

Player pushing box onto the Pressure Pad, jumps up to the upper level, and escapes to the next scene.

To allow the Player to push the box, we will start in the Player script with a new Public or SerializeField Type Float variable name pushPower which will give the speed at which the box will be pushed.

Then inside of the OnControllerColliderHit() method, we want to check IF we hit the Moving Box by its Tag. If the Moving Box was hit, create a new variable Type Rigidbody name box which will be set to Get the Component of the Rigidbody attached to the Collider that the controller hit.

Check IF the box is equal to NULL OR the box is Kinematic. If either condition is true, we want to use RETURN which will terminate the execution of this method and do nothing. Also, we want to check IF the direction our Character Controller is moving doesn’t push the box below the Player on the Y-axis less than -0.3f. If the Player does push below in the Y-axis, RETURN and do nothing to the box.

Now for the box to be pushed in a direction by the Player, let’s create a new Type Vector 3 variable name pushDirection. PushDirection will be set to a new Vector 3 with the box hit being moved in the X-axis direction only. Then, use velocity on the box by multiplying the pushDirection by the pushPower.

Player allowed to push the box a certain distance before it can’t move anymore.

Create a new script name Pressure Pad, then use the OnTriggerStay() method to check IF the other object colliding is the Moving Box by its Tag. If so, create a new variable Type Float name distance set to get the Vector 3 Distance using the Pressure Pad’s position against the Moving Box’s position.

IF the distance is less than the box coming off the pad, we need to stop the box directly on the pad. Create another variable Type Rigidbody name box that will Get the Component of the Rigidbody of the Moving Box.

IF the box is active in the game, set the box to be Kinematic so it doesn’t move anymore. Also, we want to change the color of the Pressure Pad when the box is positioned on top of the pad.

Create another new variable Type MeshRenderer name renderer which will Get the Component In Children the MeshRenderer of the Display object under the parent Pressure Pad. Check to see if it’s NOT NULL, if it’s not null we will change the material’s color of the renderer to the Color Blue. Last, we want to DESTROY the Pressure Pad to keep it from activating again to keep the Moving Box as a Static object.

When the box lands on the Pressure Pad, the box can’t move and color change to blue.

Player pushes the box onto the Pressure Pad, and the box will not move anymore. Also, notice the pad is Blue with the box on top of it.

Player pushes box onto the Pressure Pad which turns blue, and the box becomes a static object on the pad.

Just adding something to this level by creating platforms for the Player to jump up on to reach the upper area. Use the Moving Platform prefab game object, duplicate it and rename it Upper Moving Platform. Drag it into the Prefab folder, and change it to an Original Prefab. This will keep it from being a Child of the Moving Platform so it can be its own independent game object.

Platforms created for the upper level area.

Just duplicate the new Upper Moving Platforms, position them how you want the player to maneuver upward to the top area. Also, use the Point A and B game objects to make the platforms move in directions the player will have to navigate using the Moving Platform scripts attached to each object.

Platforms positioned with their assigned points.

When we play the game, we will notice the platforms not working correctly. When they collide with another object, they will be disrupted by physics used by Unity’s Rigidbody Physics Engine.

Platforms not working correctly because of their rigidbodies.

Inside the Moving Platforms game object under the Upper Moving Platforms objects, down in the Rigidbody component we will turn on Constraints to Freeze the Position and Rotation along all 3 axis. This will keep the platforms from flying around when the Player collides with them.

However a new problem will arise when the Player double-jumps into them. The Player can go through the platforms, and this is not what we want. To prevent this, turn on isKinematic to keep these platforms from allowing any objects to go through them.

Platforms positions frozen to keep them in their natural position when colliding with another object.

When we try to go through the platforms, we will notice that we’re being pushed back by physics associated with the Rigidbodies attached to the platforms.

Platforms doesn’t fly away when colliding with another object. Also, the player can’t go through the platform.

One final thing we will add to make this level a little fun is an escape exit. Create a new Empty game object name EndZone which will represent the exit the player will have to go into to escape. We’re going to zero out its positions, and move the EndZone up to the top area.

Create a Particle System selecting Effects->Particle System. Attach it to the EndZone object which will now make this a child of the EndZone. In the Hierarchy we want to change: Start Lifetime, Start Speed, Start Size, Start Color; in Emissions Tab change Rate Over Time; in Shape Tab change Shape to something like Sphere or Donut like we did.

Exit created for the player to escape from the scene.

On the EndZone, add a Box Collider and change its Y-axis Size and Center to allow the player to either walk or jump into the Box Collider.

Collider added to the particles to be a trigger for the player to go to the next scene.

Create a new script name EndZone, and attach it to the EndZone game object. Inside the script, we’re going to use Unity Engine Scene Management namespace library to go from one scene to another. Then, use OnTriggerEnter() function and simply use the Scene Manager to Load the next Scene by its index number which is 1 in this case.

One other thing we want to do is setup the scenes for the Player to go from when entering the collider. Select File->Build Settings, Add Open Scene to add the other scene created in the Scenes folder. To organize them, just click-n-drag the scenes either above or under the other scenes and close the window.

Organized the scenes in the Build Settings.

We can now escape this scene and go to the next scene.

Player pushes box on pad, jumps up using the moving platforms to reach the upper level, and escapes through the exit.

Next time we will use real game assets to have the Player move around through.

--

--