Implementing a Roll Mechanic (2.5D Platformer)!
I recently implemented this roll mechanic for the 2.5D platformer I’ve been working on. I’ll give a bit of a rundown on how I ended up approaching this 😉
1. The bulk of the edits I needed to make in order for this to work were to the Player class. Namely, I added in a check for Input.GetKeyDown(KeyCode.LeftShift) within the isGrounded statement’s scope (as this is the key I’m assigning for triggering the move) and a ‘BarrelRoll’ coroutine.
There are certainly other ways of approaching this mechanic, such as letting the animation drive the model’s movement then repositioning the Player parent GameObject accordingly, but I’ve opted for driving all of the movement within the aforementioned coroutine. I’ll admit, some of these variables should probably be declared at the class level but for now they should be fine:
I’m flipping a couple of booleans at the start and end of the coroutine, triggering a ‘Roll’ animation on the AnimatorController, incorporating a couple of delays, and lerping the player to the appropriate position over a duration of 0.85 seconds. As mentioned, some of these should probably serialised private variables so I can more easily edit them in the inspector, as currently they’re hard-coded local variables I’ve adjusted through trial and error (recompiling each time). The pause at the start and end are there so that the lerp happens at the appropriate time in the animation (i.e. not before they’ve started rolling and not after they’ve reached their end position).
2. Over in Unity, I imported a Mixamo animation the same way I’ve done previously, then added it to the Player’s AnimatorController. This time round, I’ve drawn a connection from the ‘Any State’ animation state to the ‘Stand To Roll’ animation state. I then added a trigger condition to this called “Roll” (which is getting triggered by the BarrelRoll coroutine).
With this done, I made a couple of other transitions from the roll animation state: one back to Idle (no conditions, just exit time); one back to falling (if Grounded is false); and one back to running (has exit time, but checks if speed is greater than 0.1).
With this all done, I think the only major thing I can think to mention is to ensure Loop Time is set to false on the rolling animation and that ‘Bake into Pose’ is enabled for the rotation and Y position (but not the X or Z). Have a look at what you make of the results and do adjust the speed of the animation state by clicking on it and changing it in the Inspector. You may also want to change things in the Player script to get the timings just right.
Something I’d particularly like to add to this and have only really half-implemented is that I want the animation to cut off and for the character to immediately start falling should the player no longer be grounded during the rolling animation. At the moment the player can roll off a platform and only fall after the animation’s finished. I’m having a go at fixing this now, and may write a quick article about that adjustment 😉