Mobile Game Progression
Character Dash and Air Dash
Just like with any 2D side scrolling game, you can’t omit the ability to dash and also Air dash. In this article we will create the two ability to allow better terrain travel for the player.
Preparing the Dashing sprite:
As usual, I do everything within Adobe Illustrator, but of course any available program is just as good. I took one of the default running sprite from the player run sprite sheet and isolated one frame. Then I duplicate the frame and have each following frame have a lower opacity. This simulates speed and movement.
I do the same process but this time for the jumping animation.
The beauty about animating short burst abilities is that they can technically use one frame.
Back in Unity we are going to create a new animation within the player and named them Dash and Air Dash. Just like we mentioned before, since this is a fast pace animation we only need one frame.
Within the Animator window we are going to create a new Trigger parameter called “isDashing” and we are going to set that as the condition for the dashing animation. For the return transition we are going to be directed to the running state and the condition is empty.
For the Air Dash, we are going to set up two conditions to trigger Air Dash animation. The “isDashing” and also the “Jumping” condition for true and is going to be set for the starting transition. Then for the transition back to Jump state, we are going to set the “Jumping” condition to be false.
Scripting the Dashing Mechanic:
In the player script, since we will be working with left and right dashes we need to setup two different methods for controlling the direction. But before we can start writing any scripts for dashing we need to define some variables by giving it handlers.
For Dashing towards our right, we will make sure our movement is greater than zero.
and for dashing towards the left, we will use the same formula for this time we will make our movement value be less than zero.
You might have noticed that there are several methods such as the IEnumerator “Dash” is underlined with red, that is because we haven’t created a new coroutine yet.
Create a new IEnumerator called “Dash” and also attached a float value called “direction”, this direction value is what controls the left or right movement of the dash.
We are then going to create a new public bool called “isDashing” and set the default state to be false. Within the IEnumerator we are then going to turn it to be true. We are also going to define the default gravity scale for the rigidbody which is set to 1.
Next we are going to set the new velocity for the X value of the rigidbody all while keeping the Y value to zero. This is because we don’t want the player to be dashing upward, only left and right.
Finally we are going to use the “Add Force” function for the rigidbody where we assign a new vector2 value which is the Dash distance x the direction + the another function called ForceMode2D Impulse. This impulse function allows the player to burst forward or backward.
Finally for the Air Dash, we are going to set the gravity scale to zero to let the player float a bit before making the player fall again.
We finally end the entire IEnumerator by finishing with the yield return new wait for seconds. I set the timer to a dash Time variable, we can potentially adjust the timer in the Inspector.
After the set amount of waited time we are going to turn back on the gravity scale and return the value to 1 again, then we are going to set the bool “isDashing” to be false.
Player Animation Script:
Now that the function is properly written, it is time to finish up but creating the animation methods inside the player animation script. Inside we are going to create two new method, one is the regular ground Dashing, while the other is for Air Dashing.
Just like how we set up the conditions for the Animator, we need to call those parameters within the methods exactly as is. Once these two methods have been created, the player script should be error free. When you test out your game, you will noticed that your player will be able to dash forward and back.