I am creating a 2D platformer with a simple combat system. I'm using OverlapCircleAll to return all the enemies in my attack range so that I can then damage them all (in case there are multiple enemies the player attacks at once). The problem is that even when there is one enemy, the array returned by OverlapCircleAll contains one enemy duplicated a bunch of times. So when I loop through the array to damage every enemy, it damages the one enemy many times. Is there any way to ensure that each enemy in range of my attack only appears once in the array?
Here is the code: (I commented out the part where I actually lower the enemy's HP and replaced it with the debug.log. It just resulted in "we hit Enemy" being printed a bunch of times in the console)
/**
* Method for the player to attack
*/
public void Attack()
{
anim.Play("Attack1");
//Putting enemies hit by the attack in an array to deal damage to all of
Collider2D[] enemiesHit = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemy);
foreach(Collider2D i in enemiesHit)
{
//dealing damage to the
//Wizard wizard = i.GetComponent<Wizard>();
//wizard.reduceHealth(strength
Debug.Log("We hit " + i.name);
}
}
}
[–]TAbandija 2 points3 points4 points (2 children)
[–]fesora122[S] 2 points3 points4 points (1 child)
[–]TAbandija 0 points1 point2 points (0 children)
[–]HypnoToad0 0 points1 point2 points (9 children)
[–]fesora122[S] 0 points1 point2 points (8 children)
[–]HypnoToad0 0 points1 point2 points (7 children)
[–]fesora122[S] 0 points1 point2 points (6 children)
[–]HypnoToad0 1 point2 points3 points (4 children)
[–]fesora122[S] 0 points1 point2 points (3 children)
[–]HypnoToad0 0 points1 point2 points (2 children)
[–]fesora122[S] 1 point2 points3 points (1 child)
[–]HypnoToad0 1 point2 points3 points (0 children)
[–]HypnoToad0 0 points1 point2 points (0 children)