2.5D Platformer — Rolling #13
<Insert overused pun here>
I find myself enjoying rolling for no reason in videogames that have a roll animation. There’s something to the movement that’s quite cool. I felt like coding a rolling behaviour, so here I am with a new article.
What makes your roll special?
My implementation uses a Mixamo animation. You can’t compete with that!
Jokes aside, there’s probably nothing special about it except that it changes the controller’s collider to be smaller when rolling. It’s pretty common in 3D games, as crouching in some games require resizing the collider.
This article will be mostly to describe why it’s so curious to resize capsule colliders, as the actual rolling is way too simple.
To start rolling, the player will have to press an input. In this case, Left Shift.
Once the player presses the input, the state will change, it will store the direction in which the player pressed the input, it will resize the collider, and trigger the animation.
Resizing a capsule controller requires just a small workaround to one “flaw” about it. When changing the height of the controller, it doesn’t change the way you think. Instead of going from the top to the bottom, it actually expands the height from the center of the capsule collider.
It’s a small annoyance, but math doesn’t care what I think. To fix that so the collider stays grounded when resizing the collider, is just to change the controller center position.
The animation could have Root Motion, and you wouldn’t need to code anything… But I ended up using movement through code because Mixamo. :)
Because the Roll state overrides any kind of movement, I also had to apply gravity or else the controller would float away when rolling away from the ground.
The roll ends as soon as the Roll animation ends. Using the Animator State Machine, I call the controller’s method RollEnd() to restore the controller’s collider normal size back and restore the normal movement of the controller.