x


How do i destroy a character and have a particle effect after?

I have a enemy character that comes at me and once i kill him he falls and disappears. I created a particle effect that suppose to appear right after he disappears,as if he turns into dust. how would i go about doing this? I tried to create an empty game object and attach the particle system to it which didn't work. Then i tried to attach the particle system to enemy character and i still can't get it to work. Please Help!

more ▼

asked Mar 15, 2010 at 04:39 AM

dreal gravatar image

dreal
61 18 12 18

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
  • make a gameobject
  • add particle effect to it with auto-destruct on
  • make a prefab of this gameobject
  • optionally test if particle object does it's job (create new scene, place gameobject in view, press start, you should see the effect and object should clean up)
  • modify your script to instantiate the effect:

var dieEffectsPrefab : Transform;

function Die() {
    Instantiate(dieEffectsPrefab, transform.position, transform.rotation);
    Destroy(gameObject);
}

To respond to one of the comments, the Auto-destruct should destroy the gameobject the particle is a component of so it's not really necessary to add the delayed Destroy. If you do add the delayed Destroy, you should destroy the gameObject, not the transform you get back from the Instantiate. So code would become:

var dieEffectsPrefab : Transform;
var timeOut : float = 3.0;

function Die() {
    var instance = Instantiate(dieEffectsPrefab, transform.position, transform.rotation);
    Destroy(instance.gameObject, timeOut);
    Destroy(gameObject);
}

But once again, when you check the auto-destruct in the particle animator, you don't need this extra code.

more ▼

answered Mar 15, 2010 at 05:45 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
7k 43 46 93

I think the useful thing I found in addition to Jaap's response is using: var inst = Instantiate(dieEffectsPrefab...) Destroy(gameObject) Destroy(inst, 3)

That Destroy(inst, 3) would destroy the particle effect after 3 seconds so that it had its chance to blow up and then it'd disappear.

Mar 15, 2010 at 07:15 AM duhprey
(comments are locked)
10|3000 characters needed characters left

This works for me, using ParticleSystem. Code is attached to the object we are destroying:

public ParticleSystem DestructionEffect; //assign prefab in editor or elsewhere
                                         //in code
void Explode()
    {
       //Instantiate our one-off particle system
       ParticleSystem explosionEffect = Instantiate(DestructionEffect) 
                                        as ParticleSystem;
       explosionEffect.transform.position = transform.position;

       //play it
       explosionEffect.loop = false;
       explosionEffect.Play();

       //destroy the particle system when its duration is up, right
       //it would play a second time.
       Destroy(explosionEffect.gameObject, explosionEffect.duration);

       //destroy our game object
       Destroy(gameObject);

    }
more ▼

answered Jun 01, 2012 at 07:19 AM

backalleyJack gravatar image

backalleyJack
15 3 2 3

Particle Systems normally destroy themselves, meaning that the timed destroy is normally not necessary.

You answer has turned up three times because it was held in a moderation queue, that happens when you have low Karma and have just joined the site. Someone with enough Karma will probably eventually delete the extra answers, but it would save them the effort If you did it :)

Jun 01, 2012 at 08:04 AM whydoidoit

Done, sorry about that.

When I tested this, the particle always seemed to remain and replay after its duration ran out, even with loop = false. I didn't see any auto-destroy flag or anything like that in the API either. I'm curious, how do you get them to destroy themselves?

Jun 02, 2012 at 06:42 PM backalleyJack
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1485
x1188
x992
x229

asked: Mar 15, 2010 at 04:39 AM

Seen: 6110 times

Last Updated: Jun 02, 2012 at 06:43 PM