“Funslingers” Devblog #25 | Adding a Dodge Ability in Unity
Now that we’ve created a solid base game, where there is all the functionality and core features we need to call it complete. We can start adding extra features that will further the depth of this game and really set it different than other games in it’s genre!
So, the first fun feature we can add to vary the gameplay a bit more, will be a dodge ability! If the player ends up in a sticky situation and needs to make a quick escape from a wave of bullets, the player can dodge roll away and be protected from incoming fire!
So let’s start by creating the trigger for how I want the player to activate this ability.
We’ll bind this ability to Left Shift since it’s similar to a sprint feature, and we’ll call a Coroutine for the ability since the dodge should last about 1 second, so we can program around a WaitForSeconds() delay.
We have a second if() statement for different directions, since the player should be able to dodge left or right. So we can pass in a simple integer that we can handle with a switch statement inside the Coroutine.
So in our Roll Coroutine we have a mixture of a lot of other methods we’ve used for other features. First, we activate our roll animation, then immediately set all our bools to stop the player from being able to move, shoot, take damage, and roll again. Invincibility was done through a very simple if statement I added to our LoseLife() method.
Next, in our Coroutine I have a switch statement which will take the integer representing the direction the player is moving and rotate the player to face that direction.
After that, the Coroutine will wait the approximate amount of time of the roll animation, and reset everything back to normal!
Lastly, we get our Roll Animation and plug it into our Controller!
With the way I’ve set up the animation controller, the player will only able to roll while they are in a strafe animation, so only while they are currently moving. Then once the animation is over, it will immediately return to the player’s idle animation, ready to start strafing and roll again.
With this animation in place, our player can now roll away and dodge incoming bullets!