Day 106: Create a Roll system.
Objective: While running, hit Left Shift to roll forward
First the to get will be our animation of rolling. Head to Mixamo and find the perfect roll animation you would like download and bring into Unity. Remember to set the Rig to Humanoid and make a copy of the animation. Next let’s drop that into our animator.
Before moving to the player script to roll, we will set the condition of the transition’s. We will need a trigger named Roll to start the roll animation.
We will move to our player script now. In our Movement method we will need to know when we are rolling and check for Input to Roll. As well as to check to see if we are already rolling.
After that we will set the animation trigger to start the roll animation, next disable the Character Controller, and finally set isRolling to true.
This will cause the player to roll. But after the animation is finished, the player will rebound back to the position where the roll was started. To fix this we will do the same as we did yesterday. We need a RollBehaviour script, also known as a StateMachine Behaviour. In this script we will un comment OnStateExit again and call the player script again. We will also call the method that will position the player correctly.
Back in the Player script we will need a reference of where to set the position after the roll.
Next Create the method that will set the players position after the animation is finished. In this method we will set the players transform to the _rollEndPos position. Set rolling to false and re-enable the character controller.
In Unity we will need something to tell us the new position. Create a cube under the Player game object and name RollEndPOS. Play with the animation till you find a good spot that works with your animation. This will time time to get as close as possible.
After you get it disable MeshRenderer on the cube and assign it to the player script.
And that is all there is to a Roll feature.
Thank you for reading and have a wonderful day!