- Home /
How to make enemy explode after dying?
So far I have managed to make the enemy explode using a prefab however the enemy seems to explode constantly when I start the game rather than after dying.
This is the enemy script:
void Awake ()
{
currentHealth = startingHealth;
}
public void TakeDamage (int amount)
{
currentHealth -= amount;
healthSlider.value = currentHealth;
if(currentHealth <= 0)
{
Destroy (gameObject);
}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Background")
{
return;
}
Instantiate (explosion, transform.position, transform.rotation);
}
Answer by im012 · Oct 04, 2016 at 05:14 AM
You Should Instantiate the Explosion prefab When You Destroy the Game Object Like this.. if(currentHealth <= 0) { Destroy (gameObject); Instantiate (explosion, transform.position, transform.rotation); }
Remove the instantiate lines from the OnTriggerEnter() that causes the Explosion to instantiate Constantly..
I Hope that might be useful for You @satnav222
Thank you for helping me out!
I managed to fix the explosion and now the enemies are more satisfying to destroy.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
EnemyAI C# Error 1 Answer
enemy detect player then attack - c# 1 Answer
Enemy Targetting Help 1 Answer