In video games, you have gotta go fast. Normal run speeds just won’t cut it and walking around normally is boring. For us, speed demons or parkour-inspired folks, having a dash mechanic in our game is a must.
But, the question remains is how do you create a dash mechanic? Well, to do that, we’re going to use a lot of the information and concepts we discussed in our double jump tutorial, so read it here.
A Dash vs A Jump
Fundamentally, a dash and jump are not all that different. Both are on-demand actions that add force in a direction. The key differences are the direction the force is applied and that jumping has force applied to the character after the jump is completed.
A dash often applies force in the direction the character is facing, briefly altering the speed of the character for some time. This can take on many forms, which leads me to to the types of dashes.
Types Of Dashes
You have probably played tons of games with some form of dash mechanic. With that said, here are a few:
- Increased base movement speed while a button is pressed (think Pokemon running shoes)
- Temporary Boost in Speed (think Sonic’s speed pads or games where you get a temporary boost on button press)
- Gradual Speed Increase (think old school Mario or Sonic’s old school running mechanic)
Each one is implemented with slight variations, and I’ll give you code examples of the second one, which is the most common.
The temporary boost in movement speed is similar to a jump, we add force in a specific direction(character’s facing direction). By increasing just the speed for a single frame though is not enough. We need a force acting against the speed to simulate the decay of the movement speed boost. This is why jumps work because two forces are acting upon the vertical position of the character over a couple of frames. If we did not do this, the character would have increased movement speed similar to scenario one with the Pokemon Running shoes, except permanently.
You can take a look at a live demo here.
Here’s the most important code in that section:
In the demo, we do just like the above. We increase the movement speed when the space button is pressed, this pushes the character forward on the first frame. For every other frame, the character’s speed gradually goes down to the initial speed, creating that temporary boost and then dampening it. The most important thing to understand is that it’s the two opposing forces acting upon each other across multiple frames that creates the temporary movement speed for your dash mechanic. This is based on core fundamentals of Physics and the real world.
Having that fundamental understanding by writing small games will drastically improve your understanding of how to build the rules of the world for any game you create in the future.
And that’s a basic introduction to dashing in game development; you can take the code and use it in your own game. Feel to share with your other friends. Stay tuned for more and keep creating!!