Switching Collider Is Trigger Off and On makes Player behave strangely

Hello,

I have a player object with a Polygon Collider 2D. The Collider has Is Trigger = true. I have a explosive mine attached to a chain. The chain is made up of several sprites with Capsule Collider 2D components joined together using the Hinge Joint 2D. Both the chain and the Player have Rigidbody 2D components.

I want the chain to move when the Player touches it. I understand that to do that I need to turn off the Is Trigger = true on the Player. I updated my Player controller script to do that when OnTriggerEnter2D and to set it back to true using OnCollisionExit2D.

private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Mine_Chain_Links")
        {
            this.gameObject.GetComponent<PolygonCollider2D>().isTrigger = false;
        }
    }

    private void OnCollisionExit2D(Collision2D other)
    {
        this.gameObject.GetComponent<PolygonCollider2D>().isTrigger = true;
    }

This works and allows me to interact with the chain. The problem is that has side-effects. Sometimes the Player gets stuck on the chain and can’t escape. Sometimes the Is Trigger is not set back to true when leaving the chain. Other times the Player becomes jittery or the movement controls no longer work. Sometimes the movement starts going diagonal when pressing up.

I am not sure what I am doing wrong? Has anyone seen this issue before or know how to fix?

I have been trying to resolve this all day and am getting nowhere :frowning:

Many Thanks,

John

Hello,

After a decent sleep, I woke up this morning with the answer. I keep getting told that staring at a problem when tired doesn’t help, but do I listen! :sweat_smile:

Anyway, I didn’t find anything close to my issue on here or Google, so thought I would post what I did…and it was very simple.

I added a Box Collider 2D (Is Trigger = false). I made it slightly smaller than the Polygon Colider 2D (Is Trigger = true). The reason for this is that I still want enemy bombs to destroy the player and that wont happen if the Box Collider 2D is larger than the Polygon Collider 2D because they will just bounce off.

Now the Box Collider 2D interacts with the chain and moves it and the player can still be destroyed when colliding with walls, bombs, enemies etc.

I hope that someone else might find this useful in the future.

Many Thanks to everyone who viewed the post,

John