Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question 2D Melee atack using Physics2D.OverlapCircleAll not working.

Discussion in 'Physics' started by Enkobcn, Jul 10, 2023.

  1. Enkobcn

    Enkobcn

    Joined:
    Jun 6, 2018
    Posts:
    2
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMeleeCombat : MonoBehaviour
    6. {
    7.  
    8.     public Transform attackPoint; // Reference to the melee attack point
    9.     public float attackRange = 0.5f; // Range of the melee attack
    10.     public LayerMask enemyLayer; // Layer mask to identify enemy objects
    11.     public int attackDamage = 10; // Damage amount of the melee attack
    12.  
    13.     private Animator m_animator;
    14.     private int m_currentAttack = 0;
    15.     private float m_timeSinceAttack = 0.0f;
    16.  
    17.     private void Start()
    18.     {
    19.         m_animator = GetComponent<Animator>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetMouseButtonDown(0))
    26.         {
    27.             PerformMeleeAttack();
    28.         }
    29.     }
    30.  
    31.     void PerformMeleeAttack()
    32.     {
    33.  
    34.         if (m_timeSinceAttack > 0.25f)
    35.         {
    36.          
    37.             m_currentAttack++;
    38.  
    39.             // Loop back to one after third attack
    40.             if (m_currentAttack > 3)
    41.                 m_currentAttack = 1;
    42.  
    43.             // Reset Attack combo if time since last attack is too large
    44.             if (m_timeSinceAttack > 1.0f)
    45.                 m_currentAttack = 1;
    46.  
    47.             // Call one of three attack animations "Attack1", "Attack2", "Attack3"
    48.             m_animator.SetTrigger("Attack" + m_currentAttack);
    49.  
    50.             // Detect enemies within the attack range
    51.             Collider2D[] hitEnemies =  Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayer);
    52.  
    53.             Debug.Log("Number of hit enemies: " + hitEnemies.Length);
    54.  
    55.             // Deal damage to each enemy
    56.             foreach (Collider2D enemyCollider in hitEnemies)
    57.             {
    58.                 Debug.Log("Hit");
    59.                 // Retrieve the EnemyFollow script from the enemy object
    60.                 EnemyFollow enemy = enemyCollider.GetComponent<EnemyFollow>();
    61.                 if (enemy != null)
    62.                 {
    63.                  
    64.                     enemy.TakeDamage(attackDamage);
    65.                 }
    66.             }
    67.  
    68.             // Reset timer
    69.             m_timeSinceAttack = 0.0f;
    70.         }    
    71.     }
    72.  
    73.  
    74.     // Visualize the attack range in the editor
    75.     void OnDrawGizmosSelected()
    76.     {
    77.         if (attackPoint == null)
    78.             return;
    79.  
    80.         Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    81.     }
    82. }
    83.  
    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.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,177
  3. Enkobcn

    Enkobcn

    Joined:
    Jun 6, 2018
    Posts:
    2
    Thanks! It was that and also that I did wrong with my counter.
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,177
    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<T> 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.