Unity Guide

Pushing objects to complete puzzles | Unity

A quick guide about how to push objects and trigger a pressure pad to complete puzzles in Unity

Fernando Alcantara Santana
Nerd For Tech

--

Objective: Implement a system for the player to push objects and trigger a pressure pad in a platformer game with Unity.

In the last post I covered how to implement a wall jumping mechanic with Unity. Now, it’s time to implement a system to allow the player to push objects and trigger a pressure pad that could work to complete a puzzle in our platformer game in the future.

Current stage

To start, let’s take a look at the stage that we’re going to use in order to implement the push and trigger mechanic:

The stage is intended to allow the player (at the right) to move to the left and push the cube until it reaches the yellow pressure pad and provides a way to build a future puzzle in our platformer game:

If we check the components of these gameobjects we’ll see that:

  • The cube contains a normal box collider and a rigidbody that is affected by gravity.
  • The pressure pad contains a box collider with the Is Trigger property enabled to identify when other colliders enter its collider.

Pushing objects

And now, in order to start implementing the pushing mechanic, let’s start by tagging the cube with a special tag:

Then, let’s open the Player script and create a new variable to indicate the power to be applied when pushing the objects:

You can use [SerializeField] to modify the private value through the inspector.

Once created, let’s modify its value through the inspector:

Next, let’s use the OnControllerColliderHit method to:

  • Identify when the character controller of the player collides with another collider. In this case, when it collides with a collider tagged as MovableBox.
  • Get the rigidbody attached to the collider by using the attachedRigidbody property from the collider identified.
  • Check if the rigidbody isn’t null.
  • Determine the horizontal direction to be applied to the collider by using the moveDirection property of the hit.
  • Set the velocity of the rigidbody from the other collider by multiplying the direction by the pushing power of the player.

If we run the game with Unity we’ll see that the player moves the cube when it collides with it:

But what if we want the pushing mechanic to slide the cube? Well, we can select the cube and freeze the rotation of the cube within the Constraints of its rigidbody:

This way we’ll be able to push the object and avoid rotating it:

Triggering the pushed object

Now, in order to trigger an action when our pressure pad detects the cube, let’s start by creating a new script for it:

Then, let’s open the script and create a new private variable to store a reference to the Mesh Renderer component of the pressure pad:

Next, let’s use the Start method to initialize the variable and check if it’s null as a good practice:

Then, let’s use the OnTriggerStay method to identify when a collider stays inside the collider of the pressure pad. In this case, let’s check if the collider belongs to the box that the player is pushing by using the CompareTag method:

Now, in order to identify when the cube is within a range to activate the pressure pad correctly, let’s get the distance between the cube and the pressure pad by using the Vector3.Distance method:

Note: We can use Debug.Log to print the distance and check which value works for us.

Finally, if the cube is within the desired range, let’s:

  • Obtain the rigidbody from the other collider by using the attachedRigidbody property.
  • Check if the rigidbody isn’t null.
  • Set the rigidbody to be kinematic in order to stop applying any Physics to it.
  • Change the color of the pressure pad by modifying the color property of the material.
  • Destroy the script component once the pressure pad is triggered.

And now, if we run the game with Unity, we’ll see that the pressure pad works as expected when the player pushes the box over it:

And that’s it, we implemented a system to push objects for our platformer game with Unity! :D. I’ll see you in the next post, where I’ll be showing how to upgrade our project to use the Universal Render Pipeline (URP) with Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).