Pushing Objects to Solve Puzzles in Unity

Mike Brisson
3 min readJun 22, 2021

--

Puzzels can add can add depth to your game as well as adjust the pace. A simple way to implement puzzle logic is by pushing objects.

The first step is to make sure your block has a rigid body and to disable the rotation:

To limit the ability to move everything, let’s add a tag to the block called “Moveable”:

We will be using the character controller component to detect collisions so make sure to add that component if you haven’t already

Next we need to edit the player script, specifically the OnControllerColliderHit method to detect if the object we push against can be “pushed”:

At this point, we are able to push any object that has a rigid body (not set to kinematic) with a Tag of “Moveable”. If you set your _pushPower to 10, it would look something like this:

You now have an object that can be moved. We can take it a step further by having a trigger or “pressure pad” that can be used to stop the motion of the object as well as set up animations, color changes or even cutscenes.

Let’s set up another cube with a collider set to “Is Trigger”:

This object will act as our trigger pad. What we want to do is to determine if the moveable object is within a range of the trigger to allow for some leeway. We can use the Vector3.Distance() method to do this in a debug.log to find a value that we are comfortable with.

Let’s create a TriggerPad script to do this for us by using the OnTriggerStay method:

In this example, I found that a float of 2.02 worked well for me. I’ve also set a bool (_detectTrigger) to false so that the distance isn’t being checked anymore and I’ve set an action to occur which could be a door opening, or anything that you want to take place when the object has been placed on the trigger:

--

--

No responses yet