Making a Dash Mechanic for your game

Naz
4 min readJan 3, 2022

--

DASH! Ups… Sorry… Got too excited. As you could tell, today we are going to be developing a dash mechanic for our game!

Hello everyone, it’s Naz back, here again, this time things are about to get fast! Stick around if you want to know how to make an amazing dash for your own game!

Dashing in games is usually recognized as a very small moment where the player’s speed is enhanced a lot and it almost looks like the character teleported forward for a small distance. It is designed to make the game feel faster and make the gameplay sometimes more complex and flexible and that’s exactly why we will be adding it to our game.

DEVELOPING THE DASH MECHANIC:

The first thing we got to do is create a script called PlayerController since if you want to add it to your game you can have all the player movement inside that script but for now we will focus on the Dash part of the code.

Then we can begin to develop our mechanics. Let’s start by creating some variables. We will need a Rigidbody2d, a start dash time which will help with the time we will be dashing, as well as the dashTime variable. As our last variables, we’ll need to add our dash’s speed and a bool to check if the player is dashing. I’ve also implemented a simple animation so that this is a little bit more entertaining to watch but we’ll go deeper into animations in another article.

public Rigidbody2D rb;
bool dashing = true;
public float startDashTime;
public float dashTime;
public float dashSpeed;

Once we have the variables in place, we are left with creating a function to handle the dashing. There are many approaches to this mechanic but I chose this one: We are simply going to enhance the player velocity a small interval after the player has pressed the shift button and the WASD keys to determine the direction in which he wishes to dash towards. After he’s launched in that direction we’re going to emulate air friction. None of this will affect our movement because it is done by translating the game object and the dash is handled through the Rigidbody2D. Now, this code will resemble the movement code since we’ll simply increment the velocity. The only thing different is that now we got to check if the dash has ended before we start a new one. Otherwise, if the dash has started we have to start decrementing its time so that when it reaches 0 it stops going forward with such velocity.

void PlayerDash() {

Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 direction = input.normalized;
Vector2 velocity = direction * dashSpeed;
if (Input.GetKeyDown(KeyCode.LeftShift) && !dashing) {
dashing = true;
rb.velocity = velocity;
startDashTime = Time.time;
}
}

To recreate air friction, we need to determine its force, and for that, we need a variable.

public Rigidbody2D rb;
bool dashing = true;
public float startDashTime;
public float dashTime;
public float dashSpeed;
public float airFrictionForce;

I’ll leave that uninitialized to allow further changes inside the inspector. Moving on to the actual function that will apply the force onto our Rigidbody2D. If that small interval we determine is gone, then we’ll add a force in the same direction but on the opposite way of our velocity, multiplied by the airFrictionForce, and of mode Force.

void AirFriction() {
if (Time.time > startDashTime + dashTime) {
rb.AddForce(new Vector2(-rb.velocity.x * airFrictionForce, 0), ForceMode2D.Force);
}
}

The last thing I would like to do is: check if the player has collided with something and if so, stop him, even if it is the ground in this case. Of course, this is the way I’m doing it for my game but I will leave any further restrictions or quirks up to you to decide.

private void OnCollisionEnter2D(Collision2D collision) {
rb.velocity = Vector2.zero;
}

Okay, we are all set now, just call these functions in the update method and run it!

void Update() {
if (rb.velocity.x == 0) {
dashing = false;
}

if (canMove && !dashing) {
PlayerDash();
}

if (dashing) {
AirFriction();
}


}

When coming back to unity don’t forget that you might want to make your Rigidbody collision detection continuous, if you haven’t so already because in many cases this allows you to still hit objects around your game even when dashing. After all, let’s be honest, we don’t want the player to have superpowers and go through walls with a dash… Unless… that’s your game idea, then SURE! GO FOR IT!

Player Rigidbody2D component

I guess that wraps it up, everyone! We accomplished a lot today and added an amazing mechanic to our game. Thank you for coming along this journey and see you next time!

--

--

Naz
0 Followers

Hello everyone! I stream game development and gaming. If you enjoy any of those then join the Legion!