2D Physics: Can rigidbody ignore colliders if it starts inside them?

69215-example.jpg

Hi,
Does anybody know if it is possible to set up a 2D Physics Rigidbody to ignore Edge or Polygon Colliders if it is already inside them?

(A colliding plattform can consist of more edge colliders sequenced together, all in clockwise direction)

Thanks,
Marko

You could do a raycast from the collider’s center into the direction of the GameObject’s center. And then see if you hit the the collider or the GameObject first.

In fact, there are many ways you could check if your GameObject is within or out of your collider.

You could have the collider set to be a trigger at first, and use OnTriggerExit2D to turn on the collider portion again using Collider2D.isTrigger = true. If it still needs to collide with other objects while in the zone, you could create another layer to temporally place it in, which could collide with other objects while ignoring the starting collider. You can change what layers collide by going

Edit → Project Settings → Physics
2D

The second method might work better for what you are looking for, but the first one is great if you need specific layers for something mechanics-wise.