How To Add A Double Jump To Your Character Controller
This article builds off of the “Physics-Based Character Controller” that I did in my last article https://adamwreed93.medium.com/creating-a-physics-based-character-controller-in-unity-82063c8e23ad
If you are new to Unity and/or computer programming and haven't gone through it yet, I would recommend it.
Now on with the show!
When you are making a game, there will be plenty of times in which you think to yourself “Hmm, this mechanic would go really well in this game.”, and you may be right. But it is important to look at your game’s core mechanics, the results you’re trying to achieve, and the demographic you’ve decided to target when you make these decisions.
Look at the original “Super Mario Bro’s” game as an example. This game is almost all about jumping. You jump to clear obstacles, jump to grab coins, jump to hit blocks, kill enemies, and even winning the level involves one final jump onto a flag. This is a prime example of a famous design principle that Nintendo is famous for using known as “Bottom-Up Design”.
“Bottom-Up Design” is where you begin designing your game based around an entertaining mechanic and build off of it from there instead of building off of an idea such as a story, or character like most games do.
Notice though, that Mario has never really had a “mid-air” double jump. He can jump consecutively in sets of three to reach greater heights in the later games, and even has wall jumps, but never a second mid-air jump. Why is that? Well, I can’t speak for Nintendo, but I believe that the easy and obvious answer is that the developers didn’t feel that it would support their end design mechanics. Maybe it’s because after testing the game, they found that it takes a significant portion of the challenge away. Or maybe they felt that the double jump would scale the complexity of their game to a point that it begins being less fun for a younger audience and thus pulling from their target demographic. It’s not that the double jump doesn’t have value, but SOMETIMES, it just doesn’t fit.
BUT!…. Today, we are making a game that CAN benefit from it, so we’re adding it in lol.
So the mechanics involved in making a second jump while mid-air is actually quite simple. If you recall the script from my previous article…
This script adds horizontal movement, gravity, and a single jump to your character controller.
But in order to create a double jump, you’ll need just a couple more parameters.
“_controller.IsGrounded” is a built-in function that checks to see if the bottom of the “Character Controller’s” collider is touching the ground. In the script example above, you’ll see that that there is an if/else check already checking if the player is grounded.
You’ll need to add a new boolean to your script to check if you can use the double jump. This will be ticked false when grounded and become true after you use your first jump. Then It’ll become false again after you do your second jump.
In order to do this, you will need another if statement to check if you’ve pressed the spacebar after you are no longer grounded.
And there you have it!