- Home /
Make a particle effect on enemy death
Hi i am trying to make an explosion effect when an enemy in my game dies. i am using raycast shooting and damage scripts to attack and kill the enemies. I've made a code to check if the enemy is dead and then Instantiate a particle system to the enemies position, but when i kill the enemy he just pops away rather than exploding. i am fairly new to programming so forgive my somewhat sloppy code.
#pragma strict
var Health = 100;
var Effect : Transform;
function Update ()
{
if (Health <= 0)
{
Dead();}
}
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
}
function Dead()
{
Destroy (gameObject);
}
if (Dead) {
var boom = Instantiate (Effect, transform.position, transform.rotation);
Destroy(boom.gameObject, 10);
}
Answer by HuskyPanda213 · Oct 28, 2014 at 04:49 AM
The problem is first, the if statement is out of a function; and second, you need to make sure that the particle is instantiated before the enemy is destroyed. Also, you do not need to check if health
This code should fix the issue:
#pragma strict
var Health = 100;
var Effect : Transform;
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
if (Health <= 0)
{ Dead(); }
}
function Dead()
{
var boom = Instantiate (Effect, transform.position, transform.rotation);
Destroy(boom.gameObject, 10);
Destroy (gameObject);
}
Your answer
Follow this Question
Related Questions
Help with my Light Flicker Script... 1 Answer
Freezing effect on camera 0 Answers
About Flare Effect - HELP - how to use 1 Answer
Unitys Particle System wont work like i want 0 Answers
How to hide part of a gameobject? 1 Answer