Can Dash only once 2D

Hello everyone I have implemented a dash mechanic in my 2D side scrolling project and I got it to work. The thing is it only works once. I think it is because the isDashing variable isn’t going back to false. I am just having a hard time understanding why. If anyone could help me that would be amazing! Have a great day!

Kindly,

Harpoaian

here is my dash function
void Dash()
{

        if (onGround == true && Input.GetKey(KeyCode.F) && isDashing == false  && thePlayer.DashTimer() <= 1.0f)
        {
            float dashDirection = Input.GetAxis("Horizontal");

            float dashSpeed = 500;

            thePlayer.UpdateDashTimer();

            GetComponent<Rigidbody2D>().velocity = new Vector2(thePlayer.MaxDashSpeed() * dashDirection, GetComponent<Rigidbody2D>().velocity.y);
            isDashing = true;
            Debug.Log("I am dashing!");
            Debug.Log("DashTime = " + thePlayer.DashTimer());
            Debug.Log(isDashing);
            rb.AddForce(transform.right * dashSpeed);

        }

        else if(isDashing == true)
        {
            isDashing = false;
            Debug.Log(isDashing);
        }

   

    
}

Without seeing the rest of your code, I’m not exactly sure why your method isn’t working. However, here’s a potentially simpler way to do it:

public float dashCooldown = 1f; //How long the player has to wait before they can dash again

private float lastDash = 0f; //Amount of time since we last dashed

void Update () {
    checkDash();
}

private void checkDash() {
    lastDash += Time.deltaTime;
    //If we've waited long enough and the player presses the dash key
    if (lastDash >= dashCooldown && Input.GetKeyDown(KeyCode.F)) {
        lastDash = 0f;
        //I've pasted some of your dash code here:
        float dashDirection = Input.GetAxis("Horizontal");
        float dashSpeed = 500;
        GetComponent<Rigidbody2D>().velocity = new Vector2(thePlayer.MaxDashSpeed() * dashDirection, GetComponent<Rigidbody2D>().velocity.y);
        rb.AddForce(transform.right * dashSpeed);
    }
}

I’ve done away with isDashing, because we can just check the timer to see if we can dash again. Also, I recommend making dashSpeed public and outside of any methods, so you can easily change it in the inspector.

I’m at work, so this is untested code. Feel free to come back with any questions. Good luck!

@MattG54321 Hi there, is there any way to convert this to 3d?

I feel it is this portion here -

                // detect input movement
                var moveHorizontal = Input.GetAxis("Horizontal");
                var moveVertical = Input.GetAxis("Vertical");
                IsMoving = moveHorizontal != 0 || moveVertical != 0;
 
                IsRunning = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
                if(IsRunning)
                {
                    speed = runSpeed;
                }

If there is any way you can help it would be greatly appreciated man. Been at this for days now. I’ve changed a few 2D scripts, and one really helped me understand it a bit more, but actually implementing it is impossible with my knowledge.

Thanks for you time