IEnumerator HandleDash(float dashTime = 0.5f) { if (!grounded) { airJumpsLeft++; } if (!dashing) { yield break; // If dash was cancelled already, don't redo the function when the dash would naturally end. } // Set the and remove gravity before waiting the full dash duration. GetComponent <Rigidbody2D>().velocity = new Vector2(moveSpeed * dashSpeedMultiplier * transform.localScale.x, 0); GetComponent <Rigidbody2D>().gravityScale = 0; yield return(new WaitForSeconds(dashTime)); // Lets you control how long dash lasts. // Stop all of the velocity then wait, suspending the player in the air for a fifth of the dashTime. GetComponent <Rigidbody2D>().velocity = new Vector2(0, 0); yield return(new WaitForSeconds(dashTime / 5)); // If stoptime is 0.5, becomes 0.1 secs. If 0, then no wait. // After being suspended, the player regains control and gravity returns. GetComponent <Rigidbody2D>().gravityScale = 3; dashing = false; // Release particles to make exiting the dash more impactful. particle.DashParticleRelease(this.transform.position); }