- Home /
How to check if input was made x seconds/frames ago?
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;
}
}
perhaps a side route to the solution: It sounds like that input buffer is not "consu$$anonymous$$g" the keys when you check them. $$anonymous$$eep in $$anonymous$$d I know nothing about that library, but often this is because you need to call a different function to both get AND "consume" the last keypresses.
another possible solution, that does not answer the question about time.
hold them down for more than a frame, the airdash code gets run again
Sounds to me like you should set a flag when airdash is called, and only clear it when the airdash operation is done. Then check this flag at the very begining of you airdash function: if it's set, just return. I guess this flag COULD be a time value, rather than a bool. Rather than checking if true, you could compare it to the current time.
The reason I didn't answer the question about time is, I think this will require you to RECORD the time of each keypress, essentially making your own parallel input buffer, but with times rather than keystrokes. This would be a bit of a project.