How to push objects with Character Controller in Unity
Unlike transform.Translate, the Move function on the Character Controller does not affect objects that have physics enabled. Let’s have a look at how to make the Character Controller have the same behaviour.
I created the Player script in this article and made further changes to it in this article.
Open the Player script. Add a serialized float for the amount of force the player will push with. Inside the OnControllerColliderHit function check if the object colliding with the player has a RigidBody and if it does, also check whether isKinemaic is enabled. Apply force to the object with a RidigBody:
In case you do not want the cube to roll, you should enable the constraints on the RigidBody component:
As a basic puzzle you can set up a “pressure pad” to push the box onto. To do this, you will only need two cubes, one for the parent to hold the collider and another for the “pad” visuals. Remember to enable isTrigger on the collider:
Create a script called Pressurepad and attach it to the parent cube:
Open up the script. Add an OnTriggerStay function. If the object colliding has the tag of Box, check the distance of this object. When distance is less than 0.05f, enable isKinematic to stop it from moving, and change colour to green to indicate success. After 2 seconds destroy the box:
Final result:
Good luck!