Pushing Objects in Unity to Complete Puzzles

Hernando Nieto Jaramillo
2 min readAug 13, 2021

In this part of the game, we need to move a cube to advance

There are some interesting things to keep in mind:

  • The player has a CharacterController, not a Rigidbody
  • The cube has a Rigidbody

At first glance, in my opinion, it should not be possible to move the cube, because it should be like to move an object with a mass of 1 (cube) with an empty one (player)

CharacterController has an OnControllerColliderHit(ControllerColliderHit hit) with a Rigidbody otherRb = hit.collider.attachedRigidbody. This way I can check it the other gameobject has a Rigidbody

After testing with Debug.Log(hit.moveDirection) andDebug.Log(hit.moveDirection.x) , I got these info

So, when I’m hitting the cube, the hit.MoveDirection.x is -1

In Unity doc says about ControllerColliderHit.moveDirection : “The direction the CharacterController was moving in when the collision occured”

It means that at the moment the player collided with the cube, the player’s direction (Vector3) was (-1,0,0), so moving to the left

Why is it necessary all that info?
Because, in my opinion, it’s like a little hack that will work properly in the game.

We are going to apply velocity the cube to push it with its own Rigidbody

Upssss! XD

Freeze the rotation values in Rigidbody/Constraints

Nice… XD — - gif link

--

--