- Home /
How to ignore collisions with certain objects under a condition temporarily?
I have a player with obstacles on the track. Normally, if the player collided with the object, the player would take damage or do whatever to slow down his progress, but if the player has a powerup, I want to make the player ignore the collision and just continue without slowing him down. I still want to detect the collision, because I want to play an animation on the obstacle flying away, but the actual "collision" I want it to ignore. Is there a way to do this? I hope I am clear about this. Thank you.
I also tried
Physics.IgnoreCollision(collision.collider, GetComponent<SphereCollider>(), true);
However, that would make the collision happen, and then then the ignore collision would be true. And also, I would have no way of turning this off afterwards
Answer by jzerox8k · Sep 27, 2021 at 11:40 AM
@toishaanpatel It's a bit late, but the way I got around this is by adding another Collider2d
component to the object and setting it to be a Trigger.
Then you can use the OnTriggerEnter
, OnTriggerExit
, and OnTriggerStay
Messages to detect when an object interacts with the trigger without using collider physics. It might be a bit annoying doing this for every object but it works.