Keep the Momentum Rolling! Creating a “Roll Under” Mechanic in Unity Using the New Input System

Luke Duckett
Games Development Journal
4 min readSep 18, 2021

In this article, we are going to expand the functionality of our character controller from the previous articles to add a mechanic for a player to roll under a low wall.

The basic idea of what we are going to do is to allow the player to press left shift while they are moving to roll. This will trigger an animation and adjust the collider of the player, reverting once the action is complete.

To set up I have created a wall to block the player's path and just left one corner clear.

The first step is to download a roll animation, I got mine from the Unity asset store. The animation I use does not utilize root motion.

Let's set up the animation next. I have set it up so the player can only transition to rolling from the running animation. We will do this via a trigger. At the end of the roll, we will transition to the most appropriate animation depending on if the player is moving or is idle.

Next, let's set up the scripts. We will create a behaviour script that will fire an action when the animation is complete (I’ve called my behaviour script RollAnimation and the action OnRollComplete). The set-up for these is the same as in my article on the ledge grab system.

Next, we can add a function to the player controller script which will read the input action. I have called mine ReadRollAction. We will set this function to only fire when the player is grounded (not jumping) and they are moving. We will take the absolute value of our _xdirection and if it equals 1 (meaning we are moving) we can call a new function called “Roll”.

In our roll function, we want to first set a new bool (_rolling) to true, then we adjust the collider on the character controller so it is half the size and height. We will then set a new float value called _rollDirection to our current _xDirection. We are doing this to force the character controller to keep moving in the direction they are rolling. Then finally we trigger our animation.

In our movement function, we use our rolling direction for our z-axis while the rolling bool is set to true.

Next, we will create a RollComplete function. We set the character controller collider back to its original values and set our rolling bool to false.

We will subscribe to the OnRollComplete event and call the RollComplete function.

Next, let's add a new input action for rolling. We can open up our input actions asset and add a new action. I will call mine “Roll” and assign it to the “Left-Shift” key as a button.

Now we can assign this action to trigger our ReadRollAction function. Do this by opening up the player input component. From here click “Events” > “Player” and under “Roll” click the plus button. This will allow you to add the player object to the run time object box. Now you can select the ReadRollAction function into the function box.

Now let's see it in action.

That’s all for now.

--

--

Luke Duckett
Games Development Journal

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology