Question by
MichaelABC · Mar 06 at 08:30 PM ·
collider2dforeachrangeoverlap
How to loop through colliders in OverlapCircle range
I meant to loop through all the colliders inside the range of this overlapCircle and do something (EnemyDeath) if the colliding objects meet some specific requirements (have a script attached). However what happens is that the overlapCircle range gets ignored and instead the EnemyDeath occurs when the object with the box collider of the object with the script collide with the box collider of the enemy. Not at all what I intended.
public float circleRadius = 6f;
private void CheckForDestructible()
{
Collider2D[] overlappingColliders = Physics2D.OverlapCircleAll(transform.position, circleRadius);
foreach (Collider2D colliding in overlappingColliders)
{
if (canExplode == true)
{
var newExplosion = Instantiate(explosionFX, transform.position, Quaternion.identity);
Destroy(newExplosion, 0.5f);
Destroy(gameObject);
if (colliding.GetComponent<EnemyHealth>())
{
Debug.Log("boom, I killed an enemy");
colliding.GetComponent<EnemyHealth>().EnemyDead();
}
}
}
}
Comment
So apparently I found out a solution: if instead of passing the public float circleRadius I write a numeric value directly for OverlpaCircleAll (radius) it's working fine. Why is this though? Doesn't make much sense to me.