2
\$\begingroup\$

I'm trying to code a dash/air dash for a 2D platformer. In my character controller's FixedUpdate(), I have various if statements that cause certain actions when a button is pressed.

To disable player input while the dash was happening, I tried putting Dash() in an IEnumerator with a simple test code like this:

IEnumerator Dash() {
    if (player.GetAxis("Horizontal") < 0)
        {
            thePlayer.GetComponent<HeroController>().enabled = false;
            yield return new WaitForSeconds(0.5f);
            move = move * 2;
            thePlayer.GetComponent<HeroController>().enabled = true;
    }
}

But since the character's physics are inside the character controller, both the player's input and the character's movements are stopped when the controller is disabled. How can I disable inputs while still allowing the character to dash?

\$\endgroup\$
2
\$\begingroup\$

You could make a bool that, if true, checks for all input. If false, you essentially turn off all input.

bool DoInputs = true;
IEnumerator Dash() {
  if (DoInputs)
    if (player.GetAxis("Horizontal") < 0)
        {
            thePlayer.GetComponent<HeroController>().enabled = false;
            yield return new WaitForSeconds(0.5f);
            move = move * 2;
            thePlayer.GetComponent<HeroController>().enabled = true;
    }
}

If you'd like to keep detecting the axis, simply store it in a variable and set this variable to the input only if the bool is true.

    bool DoInputs = true;
    float AxisH;
    IEnumerator Dash() {
      if (DoInputs)
              AxisH = player.GetAxis("Horizontal");

        if (AxisH < 0)
            {
                thePlayer.GetComponent<HeroController>().enabled = false;
                yield return new WaitForSeconds(0.5f);
                move = move * 2;
                thePlayer.GetComponent<HeroController>().enabled = true;
        }
    }

That will keep the character moving while not affecting it with input. Hope one of these suits your style. Cheers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.