Platformer: Player Animation VI — Roll

Intro

We continue to add to the player’s animation tree by allow for a roll when the player is moving and hits the left shift key.

Setup

We downloaded a roll animation from Mixamo and imported into Unity.

The original animation had some settings that needed to be adjusted because the player’s rotating around the center of mass, which is the middle of the player. This is causing the player to roll in the ground.

When we change it to move around the Feet, we get the desired roll effect we want.

Now we have roll animation happening just above the ground.

Transitions & Code

We add the Roll animation to the Animator window. The transitions should only happen when the player is running so we add the transition to and from the Run animation. We are going to need a condition for the transition from Run to Roll. We are going to use a Trigger parameter called “RollForward” and add an empty transition from Roll back to Run. This will automatically go back to Run once the Roll animation is done playing.

We do not want an Exit Time from Run to Roll, so we can instantly move to the roll and the Transition would be nice to blend the two animations.

In the Player script, we are looking in the CalculateMovement method. After calculating which direction the player is moving and flipping the player to face the direction, we will want to trigger the animation. We only want the animation to player when the player is moving, so we can use the horizontalInput to figure this out. Since we get the Raw & Absolute Horizontal Axis, each time we get press the left/right keys the horizontal will be 0.1. The first check can be if the horizontalInput is not equal 0, meaning we are moving.

Along with moving, we want the player to press the left shift key, so we will want to include this in the IF statement. IF BOTH of these conditions are TRUE (moving & left shift pressed), we will trigger the Roll animation.

Looks pretty good, but after testing it I noticed when can still jump while in the Roll animation. We don’t want this since it defies physics so we want to figure a fix out that only allows the player to jump when the player is grounded and not already rolling.

We need to know when the Roll animation is finished, so we are going to add an animation behavior script.

In the animation behavior script, we have a public method that runs when the animation is done or has exited. This is where we will want to enable the jumping.

First, we need to get a handle of the Player script, we can do that through the Animator. The Animator is on the Model, so we can grab the game object of the parent transform and get the Player component.

We preform a null check of the Player component and if we have the Player component then we will call a method from the Player let it know the Roll animation is complete.

In the Player script, we create a global bool that will let us know if the rolling animation was triggered.

When we trigger the animation, we will set this bool true.

Where we are checking for the space key press that makes the player jump, also want to check if the rolling bool is false. This will disable the jump when we are rolling.

The last thing is to create the RollComplete method we called from the animation behavior script and when it is called, when the roll animation has finished, we set the rolling bool back to false which enables the jump.

Conclusion

The roll animation is a nice additional to the player’s animation tree. There are a couple areas to improve, such as, if the player rolls they cannot change the direction mid roll or roll in place. I will have to revisit this animation, but this article is a great example on how to use Animation behavior scripts to make transitions easier. Until the next article…Cheers!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store