2D Melee atack using Physics2D.OverlapCircleAll not working.

First of all I’m a newbie so I assume I’m doing a lot of wrong things, but I’ve been serching in this forum and nothing works for me. I’ll try to provide as much as information I can so sorry if this is too long, but Im sutck with this. It seems it does not detect the enemy collider, but its set on the right Layer and with a collider set to trigger. I put the debugs to see where it fails and its on the Overlap detection.
This is my player melee attack script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMeleeCombat : MonoBehaviour
{

    public Transform attackPoint; // Reference to the melee attack point
    public float attackRange = 0.5f; // Range of the melee attack
    public LayerMask enemyLayer; // Layer mask to identify enemy objects
    public int attackDamage = 10; // Damage amount of the melee attack

    private Animator m_animator;
    private int m_currentAttack = 0;
    private float m_timeSinceAttack = 0.0f;

    private void Start()
    {
        m_animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            PerformMeleeAttack();
        }
    }

    void PerformMeleeAttack()
    {

        if (m_timeSinceAttack > 0.25f)
        {
          
            m_currentAttack++;

            // Loop back to one after third attack
            if (m_currentAttack > 3)
                m_currentAttack = 1;

            // Reset Attack combo if time since last attack is too large
            if (m_timeSinceAttack > 1.0f)
                m_currentAttack = 1;

            // Call one of three attack animations "Attack1", "Attack2", "Attack3"
            m_animator.SetTrigger("Attack" + m_currentAttack);

            // Detect enemies within the attack range
            Collider2D[] hitEnemies =  Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayer);

            Debug.Log("Number of hit enemies: " + hitEnemies.Length);

            // Deal damage to each enemy
            foreach (Collider2D enemyCollider in hitEnemies)
            {
                Debug.Log("Hit");
                // Retrieve the EnemyFollow script from the enemy object
                EnemyFollow enemy = enemyCollider.GetComponent<EnemyFollow>();
                if (enemy != null)
                {
                  
                    enemy.TakeDamage(attackDamage);
                }
            }

            // Reset timer
            m_timeSinceAttack = 0.0f;
        }    
    }


    // Visualize the attack range in the editor
    void OnDrawGizmosSelected()
    {
        if (attackPoint == null)
            return;

        Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }
}

This is my enemy prefab

With the Gizmo I can see that my range is actually in the right position.

If you think I should give any more info tell me. And sorry for my english is not my first language.

Thank you guys.

If you’re not using the overloads that specifically allow you to select whether you want to detect triggers or not via a ContactFilter2D, you need to set this globally in the Physics 2D settings.

Thanks! It was that and also that I did wrong with my counter.

1 Like

Note that you should avoid the “All” suffixed calls as they create an array will the results which ends up being left to the garbage collector and will affect your performance. You should avoid this. All physics queries allow you to pass in a List to get your results. Reusing this list means you won’t get allocations and will improve performance.

Ignore any physics query that has “All” or “NonAlloc” on it. In your case, look at the OverlapCircle call and its overloads. Note all those allow you to provide a ContactFilter2D where you can specify, amongst other stuff, whether you’re interested in triggers etc.

Good luck.