When the Player shoots, a ‘missile’ is fired. When this missile explodes (on contact), it spawns an explosion object at its location. This explosion object is picking up multiple OnTriggerEnter2D calls on enemies, which have only one collider on them. As well as this, Physics2D.OverlapCircleAll returns the SAME collider on the enemy multiple times. Here is the necessary code:
Explosion : AttackClass
void Start()
{
Colliders =
Physics2D.OverlapCircleAll(gameObject.transform.position,
0.5f);
// 0.5f is the radius of the collider
foreach (var Collider in Colliders)
{
if(Collider.tag == "Enemy"){
print("Hit enemy");
}
}
}
public override void OnTriggerEnter2D(Collider2D col){
if (col.tag == "Enemy"){
print("Triggered");
}
}
Most of the time it only prints each statement once, however when the Player is touching the Enemy or sometimes when the Player is below the Enemy and fires a missle, each print statement is printed twice and on some enemies (bigger ones) even three times. There is only one enemy in the scene. I don’t really have any idea how this is happening, but maybe it has something to do with the colliders contact points?
Zombie Enemy:
Slime enemy:
Explosion object:
As you can see my enemies only have one capsule collider on them each, which is triggering OnTriggerEnter multiple times as well as returning multiple times in Physics2D.OverlapCircleAll.
On the slime enemy it would trigger OnTriggerEnter / return multiple times in Physics2D.OverlapCircleAll one time normally and 2 times abnormally (when the Player is positioned extremely close to the enemy, or when the attack comes at a weird angle)
On the zombie enemy it is one time normally and 2-3 times abnormally. I think this is because the zombie enemy has a larger collider?
This issue has been plaguing my development for a while now and i have made a couple of threads on this with no responses.
I hope i have outlined the issue with enough detail and i would very much appreciate any type of response.
Thanks.