How do i add doublejump to my code?,How do I add doublejump to my code?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour

{
private Rigidbody2D rb;
private BoxCollider2D coll;

[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 14f;

[SerializeField] private LayerMask jumpableGround;

// Start is called before the first frame update
private void Start()
{
    rb = GetComponent<Rigidbody2D>();
    coll = GetComponent<BoxCollider2D>();
}

// Update is called once per frame
private void Update()
{
    float dirX = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

    if (Input.GetButtonDown("Jump") && IsGrounded())
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }
}

private bool IsGrounded()
{
    return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}

}

There are a lot of ways to do this, depending on your game and intentions.

One easy way is to add a counter to your jumps (or a “hasDoubleJumped” boolean), add timers to when you are allowed to double jump. Start a timer/distance check when you jump to decide how long after your jump that you are allowed to follow it up, and reset the jump counter when you touch the floor.

You get “infinite” jump if you simply turn off IsGrounded() check, so regulate that + timers to allow double jump at certain times.

By adding a boolean where you save the current double jump status. You reset it once you touch ground and add another condition to your jump check that you either have to be grounded or your doublejump is available.

In case you are not grounded when jumping you have to set the doublejump availbility flag and you are good to go.

In general there are hundreds of tutorials and example codes on this topic.

Declare a variable that keeps track of your extra jumps, then when you check if grounded you add an or to check if there are any extra jumps, then when you are grounded you have to reset the jumps. If you only want 1 extra jump you can use a bool.

bool canDoubleJump;
if (Input.GetButtonDown("Jump") 
{
     if (IsGrounded())
     {
         Jump();
     }
     else if(canDoubleJump)
     {
          Jump();
          canDoubleJump=false;
     }
}

if(IsGrounded())
    canDoubleJump=true;

Jump()
{
     rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}

And I put your jump code in its own method to prevent rewriting code.

[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 14f;
[SerializeField] private LayerMask jumpableGround;
private Rigidbody2D rb;
private BoxCollider2D coll;
private int jumpCount = 0;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();
    coll = GetComponent<BoxCollider2D>();
}

private void Update()
{
    float dirX = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    if (Input.GetButtonDown("Jump"))
    {
        if (IsGrounded())
        {
            jumpCount = 1;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
        else if (jumpCount < 2)
        {
            jumpCount++;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
}

private bool IsGrounded()
{
    return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}

In this script, a jumpCount variable is added to keep track of the number of jumps made. In the Update method, a check is made to see if the player is grounded and if the jump button is pressed. If the player is grounded and the jump button is pressed, the jumpCount is set to 1 and the player jumps. If the player is not grounded and the jump button is pressed and the jumpCount is less than 2, the player jumps and the jumpCount is incremented.

If you want just 1 jump or 3 jamps just change the number in the else if (jumpCount < 2)