void Update()
{
StartCoroutine(DamageObjects());
}
IEnumerator DamageObjects()
{
Collider2D[] collider2D = Physics2D.OverlapCircleAll(transform.position, explosionRadius);
foreach (Collider2D col in collider2D)
{
if (col.tag == enemyName)
{
yield return new WaitForSeconds(0.3f);
DamageEnemy(col.transform);
Destroy(gameObject);
}
}
}
void DamageEnemy(Transform enemy)
{
Enemy e = enemy.GetComponent<Enemy>();
if (e != null)
{
e.TakeDamage(damage);
}
}
void Update()
{
...
transform.position = nextPos;
if (nextPos == endPoint)
{
DamageObjects();
}
}
void DamageObjects()
{
Collider2D[] collider2D = Physics2D.OverlapCircleAll(transform.position, explosionRadius);
if (collider2D.Length > 0)
{
foreach (Collider2D col in collider2D)
{
if (col.tag == enemyName)
{
DamageEnemy(col.transform);
Destroy(gameObject);
}
}
}
else
{
Destroy(gameObject);
}
}
void DamageEnemy(Transform enemy)
{
Enemy e = enemy.GetComponent<Enemy>();
if (e != null)
{
e.TakeDamage(damage);
}
}
Destroy(gameObject);
после цикла foreach
:// проходим по каждому задетому коллайдеру
foreach (Collider2D col in collider2D)
{
// если это враг
if (col.tag == enemyName)
{
// ждем 0,3с
yield return new WaitForSeconds(0.3f);
// наносим ему урон
DamageEnemy(col.transform);
}
}
// уничтожаем бомбу
Destroy(gameObject);