Pushing Objects in Unity to Complete Puzzles (2.5D Platformer)

Marcus Ansley
3 min readJul 12, 2021

--

This is just a quick and easy guide on how we can implement a simple push mechanic to our Unity project using the Unity CharacterController and the OnControllerColliderHit method. I’ll also cover a little script to detect when the object’s been sufficiently pushed onto a pressure pad, in case we wanted to use this mechanic in puzzle-solving 😉

1. In our Player class, let’s take a look at the OnControllerColliderHit method again. Since this method is all about our player’s collisions with other objects, it makes a lot of sense to use this in handling our push logic (and in fact, Unity’s Documentation demonstrates how we can use it for a pushing mechanic). We can check to see whether the object we’ve collided with has a “MoveableObject” tag (which we can place on anything we want to move) and can then check for a number of other conditions:

  1. Does this object have a RigidBody component? We’ll need this in order for us to adjust its velocity in order to move the object.
  2. If there is a RigidBody, is it isKinematic set to false? If it’s set to true, the object will remain fixed in place despite our best efforts to change its velocity.
  3. Is hit’s moveDirection.y sufficient to push the box?

Assuming these conditions are all met, we can then define a new Vector3 variable (‘pushDir’), feeding in our hit.moveDirection.x, 0, and hit.moveDirection.z. We can then apply some movement to the object using RigidBody.velocity, setting it to our pushDir Vector multiplied by a ‘_pushPower’ float value (just to give it some more oomph).

2. Now we need to set up the object we want to move (in this case, a Cube). It’ll need to have a collider on it with isTrigger set to false, as well as a RigidBody. isKinematic needs to be set to false, and you may want to expand the Constraints dropdown and freeze it on all its axes (if you don’t want the box to roll or anything). Create and add a ‘MoveableObject’ tag, and that should be our cube set up to go.

3. Lastly, we’ll need to set up our pressure pad and the script to control it. The pressure pad I’ve got here consists of a BoxCollider with a cube child object (which the player can see). The BoxCollider needs to have isTrigger set to true, and we’ll need to place a dedicated script on this (‘PressurePad’).

For the script, the main thing this needs to do is get a reference to the box using OnTriggerEnter and a tag check. Then, in the Update method, I’m checking to see what the distance is like between center of the pressure pad’s box collider (i.e. the GameObject this script is attached to) and the cube using Vector3.Distance. I’m then checking to see whether the distance is less than or equal to the minimum distance. If this condition is met, we can then call any logic we have to signify that the puzzle’s been completed. In this case, I’m just going to change the colour of the pressure pad’s visual, set the box’s RigidBody to kinematic (freezing it in place), and am then destroying the script on the panel since we’ll no longer need it. For this complete script:

With all of this done, we should have a working push mechanic and pressure pad logic for our game 😄

--

--

Marcus Ansley
Marcus Ansley

Written by Marcus Ansley

Game Developer | Game Design and Literature Graduate

No responses yet