How can I check if one 2d collider (circle, capsule, polygon etc.) is completely inside / outside of a polygon collider?
As noted in the comments, you can check whether two colliders do not intersect at all with:
Physics2D.IsTouching(colliderToCheck, polygonRegion) == false;
If the two colliders are touching, then you have two possibilities:
colliderToCheckis contained entirely inside the
colliderToCheckcrosses the perimeter of the
So, if we want to eliminate case 2, then we just need to check each edge along the perimeter of the polygon.
For 3D colliders we could use
Collider.Raycastto check just the one collider against a sequence of lines, but for some reason
Collider2D.Raycast does the exact opposite thing, checking every other collider instead.
But, we can use the same
IsTouching trick as before, just using a different collider. Add an
EdgeCollider2D to the mix, setting
edges.points = polygonRegion.points. Then you can check whether
colliderToCheck is touching/crossing any edge of the polygon's perimeter, with a tolerance you can control via the
IsTouching just queries the contact state as of the last physics step. If you've moved your colliders around, you'll need to
SyncTransforms() to update the contact information. It also only works if the physics engine is checking for collisions/overlaps between the colliders in the first place, so they can't be both static or both kinematic (cases the physics engine ignores, since by marking them that way you've told it you don't want it to process those collisions).