How To Make Pushable Objects In Unity

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.

And there you have it!

--

--

--

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

WalkThrough! Kioptrix — 3 By VulnHub

Simulate Recursion Using Java Streams

Open Source Contribution?… Do It

Learn Front End Web Development

Https on localhost

FUN IN LINUX

Leaving my comfort zone

Deep reversal of a list — Recursion Problem

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
Adam Reed

Adam Reed

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

More from Medium

Zoo Cards as Scriptable Objects

Variables! — The building blocks of programming

Cross-Platform Input Controller!

Journal 144 — Unity Project Development, 2.5D Shooter