I'm using the Rewired input plugin, and it comes with an input buffer. I enabled the buffer for my directional keys and airdash button so that the player doesn't have to hit both keys on the same frame in order to dash.
However, this means that if you press both keys and hold them down for more than a frame, the airdash code gets run again. I tried to stop this by comparing Time.time
at the beginning of Update()
to Time.time
at the beginning of the coroutine, but this seems inconsistent and it doesn't feel like the right way to do it.
How would you check for the player's input time and compare it to the time since some previous input?
Here's a sample of the dash code within FixedUpdate()
//Dash
if (grounded)
{
dashCount = 0;
dashTime = 0;
}
if (dashCount < 2)
{
//print(currentTime - dashTime);
if (currentTime - dashTime < 0.1 || dashCount == 0)
{
//Double Tap Dash
if (player.GetButtonDoublePressDown("Left"))
{
StartCoroutine(Dash("left"));
}
//Button Dash
if (player.GetButtonDown("Dash") && player.GetButtonDown("Left"))
{
StartCoroutine(Dash("left"));
}
}
}
print(dashCount);
and here's a sample of the coroutine:
//Dash
IEnumerator Dash(string axis) {
dashCount++;
dashTime = Time.time;
if (axis == "left")
{
if (inDash == true)
{
anim.SetBool("dashUp", false);
anim.SetBool("dashDown", false);
anim.SetBool("Hover", false);
}
canControl = false;
inDash = true;
anim.SetBool("dashHorizontal", true);
anim.SetBool("inDash", true);
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(-dashForceHorizontal, ForceMode2D.Impulse);
rigidbody2D.velocity = Vector2.ClampMagnitude(rigidbody2D.velocity, maxVelocity);
rigidbody2D.gravityScale = 0;
yield return new WaitForSeconds(0.2f);
rigidbody2D.gravityScale = 2;
rigidbody2D.drag = 25f;
yield return new WaitForSeconds(0.2f);
if (grounded == true)
{
yield return new WaitForSeconds(0.1f);
}
rigidbody2D.drag = 0f;
canControl = true;
anim.SetBool("dashHorizontal", false);
anim.SetBool("inDash", false);
inDash = false;
yield break;
}
}