Push it to the Goal!- Game Dev Series 85

Objective: pushing objects in Unity.

Solving puzzles is a big goal in platformer game, and pushing objects is one of the action option to it.
In this article, we will create the function of pushing the object.

The concept of pushing objects

The pushing in our game would use the same function of wall jumping, by using OnControllerColliderHit().

First thing we need to do is creating a new tab and use it to the cube we are going to push.

With this tag, we can make the cube become a touchable object to Player. Besides that, in order to move the cube, we need to add a rigidbody component to it. And freeze all the rotation to prevent it rolling when be pushed.

The rigidbody is what we are going to use in script. Open Player script and create a new variable as pushing power. Then using this pushing power to its rigidbody.

In this function, we use OnControllerColliderHit.moveDirection to give the direction of pushing. And multiply with the pushing power as moving velocity.
You should be able to push the cube now.

Put it on pressure pad

Now we can push the cube, the next step is to put it on the pressure pad to pass the puzzle. We will make it stop on the pad when it is reached the specific spot.

Create a new script and attach it to pressure pad. In this script, we will use OnTriggerStay() to detect the distance from cube.

Then use Vector3.Distance to check if the distance is less than the value you want. If so, switch the rigidbody of cube to kinematic to disable the pushing.

That should make the cube stopped on pressure pad when reached the spot.

Congratulation! You finish the puzzle!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store