Pushing objects in Unity
Objective: Allow the player to push objects around that are needed to solve puzzles.
Creating Moving Box
The first thing you will do is create a tag for the object you want to move.
In the player script use the OnControllerColliderHit method to look for collider with the tag you created. When the right object is found create a vector3 to hold the direction the object was hit. Get the rigid body of the object and make its velocity the vector3 you got multiplied by a variable that holds the push power.
Make sure to lock the rotation of the pushable object so that it will not flip.
This will allow you to push the object.
This could be used for something like pressing a pressure plate.
Create a script for the pressure plate. This script will use the OnTriggerStay method to determine when the moving object is close to the center of the pressure plate. When it is it will stop the object and change the pressure plate’s color.
This could be used to trigger many different things from opening a door to shooting a hidden trap.