- Home /
[2D] Is it possible to un-ignore collisions between colliders?
Hello everyone!
I am creating a game in which, at certain moment, I need to make the player ignore certain colliders, for this I have implemented this simple code based on game object tag:
public GameObject player;
GameObject[] allGameObjects;
allGameObjects=GameObject.FindGameObjectsWithTag("tag_GameObjectToIgnore");
foreach (GameObject GameObjectToIgnore in allGameObjects)
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GameObjectToIgnore.GetComponent<Collider2D>());
Then, after some seconds I need the player to collide again with all those "GameObjectToIgnore". Is there any method to undo the "Physics2D.IgnoreCollision"?
Thanks!
Answer by ShadoX · Aug 10, 2019 at 08:11 PM
There's a third parameter in the Physics2D.IgnoreCollisions(.., .., bool) method that seems like what you're looking for https://docs.unity3d.com/ScriptReference/Physics2D.IgnoreCollision.html
I'm assuming that you should just set that parameter to false, whenever you want the Collisions to be working again.. so basically:
allGameObjects=GameObject.FindGameObjectsWithTag("tag_GameObjectToIgnore");
foreach (GameObject GameObjectToIgnore in allGameObjects)
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GameObjectToIgnore.GetComponent<Collider2D>(), false);
Thanks, I didn't realize about this third parameter. This does exactly what I was looking for.
Your answer
Follow this Question
Related Questions
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
Collision detection not working properly with 2D sprites 1 Answer
Trigger no longer registering scenery 4 Answers
Is this normal behavior for edge collider triggers (2D)? 1 Answer
Collider/contact functions on 2d game. 2 Answers