Nerd For Tech
Published in

Nerd For Tech

Pushing Objects in Unity to Complete Puzzles

Another popular puzzle mechanic is pushing objects onto a pressure pad or towards a higher ledge to climb onto. Again, the character controller comes to the rescue, and Unity's own page on the ControllerCollider holds the answer for us!

The relevant code we’re interested is:

void OnControllerColliderHit(ControllerColliderHit hit)
Rigidbody body = hit.collider.attachedRigidbody;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.

// Apply the push
body.velocity = pushDir * pushPower;

Unity basically wrote the code for us! Thanks Unity! Let’s prepare our box by tagging it as ‘MovingBox’.

Next, to comply with Unitys code, we’ll need a variable for pushing power so let’s create a global variable.

Now in our OnControllerColliderHit function, we’ll check for the box:

Now we’ll begin transferring our code. We’ll check for the rigidbody:

We’ll transfer the rest of the code but put it in in a null check.

Note we only used moveDirection.x. We’re only concerned with horizontal movement, so that’s the only axis we’ll be using in comparison to the original example. Everything else is exactly the same!

Now let’s see it in action!

It works, but oops…we don’t want the box rolling! That’s easily fixed by ticking off the rotational constraints of the boxes rigidbody.

Now let’s try it again!

Perfect! Tomorrow we’ll deal with the pressure pad!




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

Clone Of UDEMY Website

How to Solve “Instance Unreachable 1/2 check passed” issue on EC2

Intuit Persistence Service: A Paved Road for Persistence

Camera-Ready Version Instructions for NIPS, ICML, AISTATS

CDAP new landing page

GOT stopped accepting payments — howsoever sad, but this mangling around like MVC — MVVM in…

[LeetCode]#1464. Maximum Product of Two Elements in an Array

Reverse a singly linked list

Photo: Chain links

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
Esteban Ibarra

Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.

More from Medium

2D Shadows in Unity — Shadow Caster 2D Component

3rd Person Follow Virtual Camera Explained in Unity’s Cinemachine

How to Install the Universal Render Pipeline

Working with Unity’s 2D Tilemaps