Rolling Under Obstacles

Ryan Sweigart
Nerd For Tech
Published in
2 min readJul 18, 2021

Objective: Allow the player to roll under obstacles they can’t walk under.

In the player’s CalculateMovement method, if the player is touching the ground and they not currently rolling, we’ll then look for when the player presses the left shift button. This will set _isRolling to true, triggers the roll animation state, modifies the size of the character controller, and sets the character’s facing and horizontal velocity.

The modification to the size of the character controller is what allows the character to go under obstacles. It changes from a tall cylinder to a shorter one.

While the player is flagged as _isRolling, we will not change the horizontal velocity, meaning the player will maintain their speed and facing during the roll. A wall will stop the forward momentum of the roll, but not the animation — the player is committed to finishing the whole roll. We will continue to apply gravity, so if the player rolls off an edge, they’ll keep rolling and fall.

The end of the rolling animation state is what calls the player’s FinishRoll method. This method stops the player’s velocity and calls the RestoreControllerSize method, which, well, does exactly that, and flags _isRolling as false. The game now continues on as normal.

--

--