Sitemap
Jaime

Jaime

I love everything IT.

Moving a box in Unity

Nov 7, 2021

Moving boxes, pedestals, or weighted items are normally used in games with puzzles. It’s done typically to remove obstruction, launchpad for jumping to higher places or pressure plates.

This feature of pushing an object is in Unity’s documentation: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnControllerColliderHit.html

Get Jaime’s stories in your inbox

Join Medium for free to get updates from this writer.

We can adapt this to our game. First, let’s detect what is movable. In this case it will be our box. To delineate this from the rest, we tag it as Moving Box.

Press enter or click to view image in full size

This moving box needs to have Rigidbody to be pushed. We also want to control how it is moved. For example, we don’t want it to roll when moved. Since this is the case, we freeze the rotation:

Next we add velocity to the moving box to cause it to move. You also do not want to push object if they are below the Player:

Now, the Player can move the box.

Press enter or click to view image in full size

--

--