user9173997
user9173997

Reputation:

Unity - OnCollisionStay2D Not working as properly

I want my Character to ignore the layer is colliding with when I press S (just like in Gun Mayhem when you press to go down while colliding with a layer and the character ignores the collision with that layer). The Problem is that the OnCollisionStay2D functions only works while I'm moving. So I can only ignore the collision with the layer I'm colliding with if my character is moving. Otherwise, it doesn't work. Here is what I have in order for the character to ignore the collision my code:

void OnCollisionStay2D(Collision2D c)
{
    Debug.Log("COLLISION");
    if (Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.DownArrow))
    {
        Physics2D.IgnoreCollision (GetComponent<Collider2D> (), c.gameObject.GetComponent<Collider2D> ());
    }
}

Upvotes: 2

Views: 4261

Answers (1)

Brian Green
Brian Green

Reputation: 81

On your character rigidbody2d, set 'Sleeping Mode' to Never Sleep. This will allow your character to detect Collision every frame regardless of movement.

https://docs.unity3d.com/ScriptReference/Rigidbody2D-sleepMode.html

Upvotes: 6

Related Questions