How to use OverlapCollider to check for trigger colliders

I want the method Collider2D.OverlapCollider to be able to only check for trigger colliders. However, even after reading the scripting reference for the ContactFilter2D, i am still not able to understand how to use it. Does anyone know how to set the contact filter?

This is my current code: (colList is a list created to store a list of colliders)

ContactFilter2D c = new ContactFilter2D(); c.useTriggers = true; https://docs.unity3d.com/ScriptReference/ContactFilter2D-useTriggers.html

2 Answers

2

I know this is an old question, but I figured it out for anybody else who comes across this. You need to specify your ContactFilter2D has no filters:

        List<Collider2D> colliderList = new List<Collider2D>();
        ContactFilter2D contactFilter = new ContactFilter2D();
        int colliderCount = myCollider2D.OverlapCollider(contactFilter.NoFilter(), colliderList);

If you pass in an empty ContactFilter2D, it will not check for Triggers, however if you specifcy NoFilters(), it will.

Thanks for the answer!! This works!

I ran into the same issue. By default, ContactFilter2D already filters triggers (which I find very counterintuitive - if I forget to configure a filter, just don't let it filter anything?). An alternative to first calling NoFilter() would be to set useTriggers to true. var filter = new ContactFilter2D(); filter.useTriggers = true; I feel they should have named it filterTriggers instead, in which case it would've been fine set to false by default.

Making the ContactFilter2D public and configuring it in the inspector is easier / could help learn parameters :0)