How would I go about implementing a double jump system in this
private void FixedUpdate()
{
rigidBoddyComponent.velocity = new Vector3(horizontalInput * 2, rigidBoddyComponent.velocity.y, 0);
//the first statement should work no matter what because it's already been previously tested alone
if (Physics.OverlapSphere(groundCheckTransform.position, 1f, playerMask).Length == 0
|| (Physics.OverlapSphere(groundCheckTransformFront.position, 1f, playerMask).Length == 0)
|| (Physics.OverlapSphere(groundCheckTransformBack.position, 1f, playerMask).Length == 0)
|| (Physics.OverlapSphere(groundCheckTransformTop.position, 1f, playerMask).Length == 0))
{
return;
}
if (jumpKeyWasPressed)
{
rigidBoddyComponent.AddForce(Vector3.up * 9, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
}
Check out some Youtube tutorials on double-jumping for the basic concepts you need. Those will be:
- the notion of “have I touched ground and reset my double jump?” (a boolean that you set when grounded)
- detecting jump input at all times, and deciding:
— is this a normal jump from ground?
— is this a double jump and do I have the ability? if so, jump and clear my ability to jump again
That’s really all there is to it. Everything else is details about how to make it feel.
To go a bit further, you can have an int “maxJumps”, and each time you jump, add 1 to a “currentJumpNumber” counter. Only jump if that’s below the max. When you land, reset to 0. This way you can have any number of jumps, including single, double, etc (maybe powerup/debuff possibility).
Also, any time you see hard-coded values (like 2 and 9 in your example), consider having them be variables. That way you can easily do a powerup like “double jump height” or “3X movement speed”. Look to Invoke() to have a powerup or debuff reset after a certain amount of time. Very straightforward and adds a lot to the game.
1 Like