- Home /
2D Area of Effect Script - OverlapCircle not working
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 box collider of the object with the script collides 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();
}
}
}
}
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.
Answer by MichaelABC · Mar 07 at 02:04 PM
Oh, how stupid of me, I should have just checked the inspector and reset the value from there.
Your answer
Follow this Question
Related Questions
Physics2D.OverlapCircleAll returning colliders outside of the area 1 Answer
Use transform.position in FixedUpdate to ensure collision with objects which have Velocity 0 Answers
BoxCollider2D have an offset on collision,Collider2D has an collision offset? 0 Answers
How do you make a circular player? 2 Answers
Draw CircleCollider2D radius ingame 0 Answers