I’m engaged on my first Tower Protection sport and I’ve an issue the place the a number of enemies get hit by a projectile as a substitute of 1.
Enemy with it is collider.
The issue is that once they get shut to one another which is okay, their colliders overlap and one arrow offers injury to a number of models.
Arrow and Enemy each have set off colliders 2D
Code for Arrow.cs element
personal void OnTriggerEnter2D(Collider2D collision) {
Enemy enemy = collision.GetComponent<Enemy>();
if (enemy != null) {
IDamagable damagable = enemy.GetComponent<IDamagable>();
if (damagable != null) {
damagable.ReceiveDamage(DamageType.PHYSICAL, attackType, injury);
} else {
Debug.Log("Unit is lacking the IDamagable script");
}
Destroy(gameObject);
}
}
Looks as if collision detection and OnTriggerEnter2D is known as in the identical time and this perform runs a number of instances occasion if gameObject is destroyed. I attempted a couple of approaches however with no luck.
Thanks upfront!