Pushing Objects in Unity to Complete Puzzles

The exercise here demonstrates pushing an object in Unity using rigid body.

The project has a requirement to get an object fixated on another object to have a puzzle solved. So, there are three game object interacting here.


There are two parts to this solution.

First, the player must be able to push the object. Second, the object must get locked on the destination.

  1. Pushing the object

In the in built class above, there is a property which can be used here.


Basically, this property keeps track of the direction the controller is moving at all times.

So, once that data is available, the velocity property of the rigid body component can be altered on the box object.

Please refer the link above which shortly solves this task.

  • Add the following code in the player script.

Note: pushPower is a private variable with a float value.

2. Locking the object

This is the target position where the box shall be fixated. It can be seen that there are two object here. The big box without mesh is the pad and the yellow cube is the display.

The task is to bring the box on this display just enough so that it looks like the box got locked onto it.

For that, a simple distance method from the vector class can be used.

  • Add a script to the pad object.
  • Add an OnTriggerStay( ) method to check the collision.
  • Get a reference to the rigid body of the box object and the Mesh Renderer of the display object. (This step would allow to change the color of the display to make it obvious that the box got fixed.)
  • Destroy the pad object so that the process gets finished without looping.

That should be it :)

Here is the final result.

Thank you very much




Work in Progress.

Recommended from Medium

Approach to Solving Problems in Procedural vs Object Oriented Programming in Java

Is Kotlin Emerging as a Multiplatform Language?

Bad error message in Spark AR

Best solutions for Microsoft interview tasks. Concatenated String Length with unique Characters.

13 Reasons Why Software Engineers Quit

AWS Serverless MicroServices Composition

Why you should avoid ioutil.ReadAll in Go

Happy Dragon Boat Festival ! | Weekly Report 16

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
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

Platformer: Collectible & Status Bar

47 High Quality Plugins on Unity Asset Store-Essential Tools for Unity Developers

Unity Interface Layout

Adding a new ability to our prototype — The Triple Shot