Overlap box only matching edges of trigger collider?

Hello,

I need to check whether a zone is overlapping a collider that is set to trigger. However, i’m having a weird behaviour, it seems to match only the edges of the collider. Is this an expected behaviour? Is there a way around it? Thanks

private bool TestHangContact()
        {
            Vector2 center = _bodyCollider.bounds.center;
            Vector2 size = new Vector2(0.32f, 0.05f);
            Vector2 distance = new Vector2(0, 0);

            distance.y = (_bodyCollider.bounds.size.y / 2)
                         // - (size.y)
                         + Physics2D.defaultContactOffset
                         + 0.02f;
            var hit = Physics2D.OverlapBox(
                center + distance,
                size,
                0,
                LayerMask.GetMask("Roof"));
           
            if (hit)
            {
                return true;
            }
           
            return false;
        }

Being a trigger isn’t relevant. As you must be able to see, you’re using the CompositeCollider2D in Outline mode (edges) so why would you expect it to produce anything but edges? :slight_smile:

A bunch of edges don’t create a closed shape with an interior, it’s just a bunch of edges same as you’d get with the EdgeCollider2D.

Place a collider “inside” that region; it won’t be overlapped because there is no “inside”, only the outline.

So in short, there’s no way around giving you exactly what you asked for here i.e. edges. unless you don’t use edges and use polygons instead.

1 Like

Hello,

Thanks for your help, this would have given me headaches for days. So, do you mean i should be using the compositeCollider in polygone mode or adding a polygone collider instead? I tried both and neither seemed to work, i must be doing something wrong.

I’m not telling you what you should be using. I’m simply saying that outlines are edges and edges are not closed shapes so have no area inside them you can detect.

If you use Polygon mode in the CompositeCollider2D then those are closed shapes but you’re using Outline mode for a reason so I’m not telling you what to do, just explaining what’s going on.

Change the Geometry Mode to Polygons as a test to see.

Sorry i was about to write, now my code doesn’t work at all anymore i have to figure out why, i don’t get a match on the edges neither, it’s weird i could swear i changed absolutely nothing since i wrote my first message.

Rofl i had changed my layer name from french “Plafond” to “Roof” to make it more comprehensible here and completely forgot about it, what a moron. It now works perfectly, thanks again :slight_smile:

1 Like