0
\$\begingroup\$

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;
        }
  }
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Let's assume we have button A and button B and you want to be able to press both of them but not in the same frame.

The general way of accomplishing this is the following:

on button A key press if button B is currently pressed execute action end on button B key press if button A is currently pressed execute action end

combining the two logics you can get

on button A or button B key press if both button A and button B are currently pressed execute action end

Now, if you want to make sure the users can press the two buttons in different frames but not more than 2 seconds apart we can slightly alter the above code as follow:

on button A or button B key press if we can handle the action execute action else mark that we can handle the action and //eventually additional logic, if required start a couroutine that will mark disable the action handling in 2 seconds end

This is a generic way that can be used to solve your issue. You will of course need to customize it for your own problem.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the help. I get the gist of what you're saying and I see how it would work for a variable, but I still can't figure out how to apply this to time. How can the engine compare the time between two inputs? Should I just use Time.time like I initially tried? \$\endgroup\$
    – eternal
    Commented Jan 27, 2015 at 7:48
  • 1
    \$\begingroup\$ I don't see why you need to compare time in this specific problem. You have to look for "key down" events. That means when the user pressed the button not while it is pressed or released. In Unity you achieve this with GetKeyDown. This way your code will no longer start running multiple times but only when both conditions are satisfied (key down and both key pressed at that time). \$\endgroup\$
    – Memleak
    Commented Jan 27, 2015 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .