- Home /
fixed it myself
foreach loop in OnCollisionStay
greetings i'm trying to make hp for a wall, but i want to have each enemy hitting the wall doing damage separately (and having a separate attackcooldown timer for each one), does anyone know how i can make one?
i have this:
private int HP;
private List<Transform> enemies = new List<Transform>();
void OnCollisionEnter(Collision hit){
enemies.Add (hit.gameObject);
}
void OnCollisionStay(Collision collide){
if (collide.gameObject.tag == "Enemy2") {
HP -= 10;
} else if (collide.gameObject.tag == "Enemy1") {
HP -= 5;
}
if (HP <= 0) {
Destroy(this.gameObject);
}
}
Answer by EvilTak · Dec 17, 2014 at 12:36 PM
I didn't get what you meant by having a separate timer, but what you can probably do is add a script to your enemies (if you don't already have one). Then add a public int wallDamage
to it and subtract it from your wall's HP on collision!
void OnCollisionStay(Collision collide){
Enemy enemy = collide.gameObject.GetComponent<Enemy>(); //Enemy is the script in which you added the wallDamage int
if(enemy != null)
HP -= enemy.wallDamage;
if(HP <= 0)
Destroy(this.gameObject);
}
That way you don't need those tedious tags!
what i ment with seperate timers is that i don't want them to attack every frame. my code isn't the problem, i just don't know how i can create a timer for every enemy, and then do damage once after the timer hit's 0, then it resets to (let's say) 2 seconds.