My script works for player 1 but the exact same script does not work for player 2

I’m working on a Smash Bros similar game and the script is supposed to make the player dash.

if(Input.GetKeyDown(KeyCode.S) && isPlayer1 && Player == "Impostor" && !isDashing)
{
    StartCoroutine(DashDelay());
}

if(Input.GetKeyDown(KeyCode.DownArrow) && !isPlayer1 && Player == "Impostor" && !isDashing)
{  
    StartCoroutine(DashDelay());
}

IEnumerator DashDelay()
{
    isDashing = true;
    transform.GetComponent<Collider2D>().isTrigger = true;

    rb.AddForce(transform.right * dashForce, ForceMode2D.Impulse);

    yield return new WaitForSeconds(dashTime);
    transform.GetComponent<Collider2D>().isTrigger = false;
    isDashing = false;
}

For player 1 it works without a problem, for player 2, instead of dashing, it pushes the player 2 slightly down.

Any idea what could be going wrong?

Hi there,

Long story short:

You had one script handling dash control for two players, but seems:
(-) that you giving commands always to same RigidBody (rb);
(-) changing Collider settings always on same Collider…

Probably you need to had two Coorutines one for Player 1 and Seccond for Player 2 something like

[SerializedField] Collider m_colliderFirstPlayer;
[SerializedField] Rigidbody m_rigidbodyFirstPlayer;
bool isDashing1 = false;


[SerializedField] Collider m_colliderSeccondPlayer;
[SerializedField] Rigidbody m_rigidbodySeccondPlayer;
bool isDashing2= false


void Update()
{
    if(Input.GetKeyDown(KeyCode.S) && Player == "Impostor" && !isDashing1)
    {
    StartCorutine(DashDelay_1());
    }

    if(Input.GetKeyDown(KeyCode.DownArrow) && Player == "Impostor" && !isDashing2)
    {
    StartCorutine(DashDelay_2());
    }
}


IEnumerator DashDelay_1()
    {
        isDashing1 = true;
        m_colliderFirstPlayer.isTrigger = true;
        yield return new WaitForFixedUpdate();

 m_rigidbodyFirstPlayer.AddForce(transform.right * dashForce, ForceMode2D.Impulse);

        yield return new WaitForSeconds(dashTime);
        m_colliderFirstPlayer.isTrigger = false;
        isDashing1 = false;
    }

IEnumerator DashDelay_2()
    {
        isDashing2 = true;
        m_colliderSeccondPlayer.isTrigger = true;
        yield return new WaitForFixedUpdate();
 m_rigidbodySeccondPlayer.AddForce(transform.right * dashForce, ForceMode2D.Impulse);

        yield return new WaitForSeconds(dashTime);
        m_colliderSeccondPlayer.isTrigger = false;
        isDashing2 = false;
    }

Hi there, first of all, thank you for answering!

I tried your method and, somehow, now both players have stopped dashing. They just get pushed down a bit upon pressing the key, which I suspect is the collider.isTrigger. So the collider should still be working fine. So it should be the AddForce/RIgidbody not working. Any idea why it was working for Player 1 and now it wouldn’t work for any of them?

I understand this is pretty vague. Does it have anything to do with the fact that they are both scriptable objects?

Hi @privatecontractor,

I don’t believe that’s the case, because if it’s in the air it wouldn’t dash down but instead does nothing. The prefab of player 2 is just the player 1 prefab flipped. I’ve tried everything with player 2 unflipped and it still wouldn’t work.

It must be something wrong with the Rigidbody because I tried something like this:

IEnumerator DashDelay()
{
    isDashing = true;
    transform.GetComponent<Collider2D>().isTrigger = true;
    yield return new WaitForFixedUpdate();

    m_rigidbody1.AddForce(transform.right * dashForce, ForceMode2D.Impulse);
    m_rigidbody2.AddForce(transform.right * dashForce, ForceMode2D.Impulse);

    yield return new WaitForSeconds(dashTime);
    transform.GetComponent<Collider2D>().isTrigger = false;
    isDashing = false;

    Debug.Log("Dash");
}

and that would still only make player 1 dash while for player 2 it only shows Dash in the console without actually dashing

That baffles me actually, as the Rigidbody should be the same on both prefabs. I even deleted the Rigidbody on 2 and made a new one, which didn’t help. I’ve attached photos of the rigid bodies of both players.

photo for player 2:

which as you can see is the same up to the spawn position.