Pushing Objects

Kinga Osińska
3 min readJul 20, 2021

Today we will learn how to push objects. The objective is to push a box on a pressure pad to complete a puzzle.

To detect if the Player collide with an object we will use OnControllerColliderHit(). Check the previous article for more information on this method. To check for a moving box we will add a new tag called “Moving Box” and add it to a cube. We also need to add a Rigidbody component.

In the OnControllerHit() method we need to check if hit a GameObject with this tag. We also need to check if the GameObject has a Rigidbody.

Next is calculating push direction.

And pushing the box. Create a new global variable for _pushPower.

Next we will add a pressure pad. Add another cube with Box Collider attached, with isTrigger checked.

Create a new script called PreesurePad. We need to detect the moving box on the pressure pad we will use OnTriggerStay() to do that. We will check if the GameObject is our Moving Box, then we need to check the distance between the pressure pad and the box. If it is small enough we will set isKinematic to true to stop moving the box, and change pressure pad’s color to green. Last thing to do is destroying the component, so the OnTriggerStay() method won’t get called all the time.

--

--