Day 98 — Implementing a Dodge Roll in Unity

Connor Fullarton
4 min readJun 21, 2021

--

Hey and welcome!

Time for a nice fun one here, we’re good and familiar with working with animations at this point and adjusting the player’s position accordingly so let’s see how to go about adding in a dodge roll animation.

First thing first is grabbing the dodge roll you like from Mixamo and going through the usual process to import it into your project.

Once that’s in your project go ahead and add it to your animator controller and create a trigger parameter called Rolling. With the transitions you’ll want one going from Running -> Rolling and Idle -> Rolling as well as Rolling -> Idle. The transition going from rolling to idle does not need your trigger on it but the other two will.

You should have something like the above now, it’s getting a bit complex looking! Once that’s in place it’s time to add some new code to the Player script:

private bool _isRolling = false;if (_controller.isGrounded == true && _onLadder == false)
{
if (_isRolling == false)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
velocity.x = 0;
_anim.SetTrigger("Rolling");
_isRolling = true;
}
}
}
public void RollingComplete()
{
_isRolling = false;
}

There is more code in the player movement but I’ve just included the new part we’re adding in order to keep things simple. Since we don’t want the player to be moving or jumping while they’re in the middle of rolling we’re creating a new _isRolling bool to make sure that the player can only do that when it’s set to false. We also don’t want the player to roll a second time while in the middle of rolling so we put our code for that in here as well.

In order to make use of that RollingComplete() method though we’ll need to add in a new behaviour script for the rolling state in the animator. Once you’ve done that add in this code:

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
var player = animator.gameObject.transform.parent.GetComponent<Player>();
if (player != null)
{
player.RollingComplete();
}
}

With that done you should have something like this so far:

Looking pretty good so far but we’ll need to work out a way to update the position when they finish the roll. Luckily with our character controller it’s pretty easy to do but first we’ll need to find out how far the player will move once the roll is finished.

Run your game and duplicate your player object making sure that you turn off the character controller for the duplicate. Now do your roll animation and pause the game just before the animation finishes. Take a note of the current x value for your duplicate player object and then move it to the original, matching up their feet placement as best you can and then take a note of the new x value you get.

Simply work out the difference between the two values and you’ll know how far you need to move the player, for me the value is 12.5f.

Now head back to the Player script and update the code for your RollComplete() method:

public void RollingComplete()
{
if (transform.rotation.y == 0)
{
_controller.Move(new Vector3(0, 0, 12.5f));
_isRolling = false;
}
else
{
_controller.Move(new Vector3(0, 0, -12.5f));
_isRolling = false;
}
}

Since we’re directly altering the player’s rotation in order to get them to face the direction we want we’ll need two separate statements here to handle the roll in either direction.

You should now have something like the above working for you which I think is looking pretty cool!

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.