#Question about ContactFilter2D I guess

1 messages · Page 1 of 1 (latest)

dire remnant
#

So, I've been searching how to use this, but it just can't seem to work as I want to, for context this is the part of the code with it :

    private List<Collider2D> colliderList;
    private ContactFilter2D enemyLayer;
    
    private void Awake() {
        areaAttack = GetComponent<CircleCollider2D>();
        colliderList = new List<Collider2D>();
        enemyLayer.SetLayerMask(LayerMask.NameToLayer("Enemies"));
    }

    private void LookForTarget() {
        closestDistance = Mathf.Infinity;
        Physics2D.OverlapCircle(transform.position, areaAttack.radius * 1.5f, enemyLayer, colliderList);

So a "ContactFilter" is needed in the "OverlapCircle" method, and yeah that's nice cause I need to get only colliders on the "Enemies" layer from it, so as I understood from the unity docs, I set things up for the filter enemyLayer.SetLayerMask(LayerMask.NameToLayer("Enemies")); but it just don't give me any results, and I don't understand why, the only way that I got results was when doing the "OverlapCircle" but with "enemyLayer.NoFilter()", and well yeah, it works and gave me every colliders haha without applying any filter...

So yeah I could somewhat use this method like that, and do the rest myself, but it would really help me if I can filter only collider from one layer (Enemies in my case)

vapid tartan
#

Also NameToLayer is wrong

#

you should use enemyLayer.SetLayerMask(LayerMask.GetMask("Enemies"));