Let’s learn how to push an object!
Have you ever played Zelda? Maybe Prince of Persia or Spyro. Many games throughout history have had you pushing some form of object out of the way, or into certain spots to open a door or trigger an event. This is a commonplace “Puzzle” type obstacle that developers use for a variety of purposes.
So how would you do this?
First, you’ll need to create a primitive cube object. Tag it and add a rigidbody. I’m going to tag mine “Moving Box”.
Lock the rotation value on the cube’s rigibody for a smooth sliding object push. If you don’t, the cube will flip over repeatedly as you push it. Though this may be exactly what you want.
So all in all, mess around and see what you like best.
Next just add the float and method in the example below to your “Player” script and that’s it! You have a fully pushable cube.
Now, you may want the player to have to push an object into a specific location such as on a pressure plate or something. To do this you could use “Vector3.Distance” to calculate how far the object your pushing is from the target area.
Just create another new game object. This can be empty or have graphics. Then add a “Trigger Collider” to it along with the script shown below.