- Home /
Damage enemy cooldown
Hey,
I have a script on my weapon that calls a Damage function on the enemy OnTriggerStay2D. The weapon has a cooldown so that the enemy only gets hit as often as the cooldown.
The problem is that the weapon cannot hit multiple enemies since the first enemy hit will trigger the cooldown. What i want is a cooldown per enemies.
Initially the cooldown was on the enemy and it couldn't recieve damage while on cooldown, but now i want to apply diferent types of damages simultaneously so this doesn't work.
Am i doing this wrong ?
Answer by Khena_B · May 31, 2017 at 07:49 AM
Well i've solved my problem by using a List, seems to work properly, example:
private List<Collider2D> hitEnemies = new List<Collider2D>();
void OnTriggerStay2D(Collider2D enemy)
{
if (other.tag == "Enemy" && !hitEnemies.Contains(enemy))
{
StartCoroutine(Damage(enemy));
}
}
IEnumerator Damage(Collider2D enemy)
{
hitEnemies.Add(enemy);
enemyHealth = enemy.GetComponent<EnemyHealth>();
enemyHealth.Damage(damageAmount);
yield return new WaitForSeconds(damageCooldown);
hitEnemies.Remove(enemy);
}
Answer by ShadyProductions · May 31, 2017 at 07:03 AM
foreach enemy that is in the trigger you will have to use a Coroutine and maybe check something on the enemy like a bool to know if he already has this debuf so you won't apply it multiple times to the same enemy. StartCoroutine(YourDamageMethod(enemy));
IEnumerator YourDamageMethod(Gameobject go) {
//do some damage to the go
}
So you're suggesting that i move the cooldown to the enemy like i did before, but have a cooldown for each attack types?
I wanted to have the cooldown on the weapon so that it could be different for each weapons that uses OnTriggerStay2D to damage the enemy.
Just use a coroutine to trigger the damage & the cooldown.
This is an example of a basic cooldown that i did for a weapon that does damage while it stays over an enemy, but the issue is that it will only hit one enemy per tick. I don't think i understand your suggestion.
void OnTriggerStay2D(Collider2D enemy)
{
if (other.tag == "Enemy" && !doingDamage)
{
StartCoroutine(Damage(enemy));
}
}
IEnumerator Damage(Collider2D enemy)
{
doingDamage = true;
enemyHealth = enemy.GetComponent<EnemyHealth>();
enemyHealth.Damage(damageAmount);
yield return new WaitForSeconds(damageCooldown);
doingDamage = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612123145im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy damage error 1 Answer
Enemy deals damage to player on contact 2 Answers
Damaging Enemy strangely not working. 0 Answers
I kill all myenemies instead of one 1 Answer
Damage on collision with player.. 1 Answer