Pushing Objects in Unity!

addam davis
3 min readOct 15, 2021

Objective: Create the function of allowing the player to push certain objects.

In many platformers there are puzzles. One common puzzle is pushing an object onto a pressure plate. In this tutorial we are going to focus on pushing the object and we will deal with the pressure plater later.

To begin we need to select our object we want to push and tag it with a unique tag.

Open the player script and create a float variable to represent the push power. Feel free to make the value what you want and you can adjust later.

We want to check for the “Moving Box” tag we just created under the controller collider hit we created in our last tutorial.

If we do collide with an object with this tag we want to grab the rigidbody of that object.

If the collider were null it would crash the game so it is always best to null check after a get component. So, check for if the box is not null.

If the box is not null we can act on the rigidbody of the object. This is where we can add out pushing with a vector 3 and control the direction of our pushing. Since this is a 2.5D game I am only going to be dealing with the x axis.

Move direction is the direction the character controller was moving in when the collision occurred.

As we did with our player we need to define the velocity of the box. Velocity is distance times speed, so we use the push direction multiplied by the push power float.

Now your player will be able to push on the objects that share this unique tag.

If this isn’t how you want the object to be pushed we can fix it by constricting the rotation of the rigid body on the box.

Now, the box will glide across the floor instead of roll.

That’s all there is to it. Don’t be afraid to experiment with your code, and I’ll see you in the next tutorial!

--

--