- Home /
How do I get particles to emit on collision?
I have a particle system on a coin, but when I roll over it particles won't emit. Here's the code. I've tried multiple solutions, and some may be there in comments. There's no errors on either way of doing it. but the effects won't appear. #pragma strict
//var coinEffect : Transform;
var Particle : ParticleSystem;
var coinValue = 1;
Particle.enableEmission = false;
function OnTriggerEnter (info : Collider)
{
if (info.tag == "Player")
{
GameMaster.currentScore += coinValue;
//var effect = Instantiate(coinEffect, transform.position, transform.rotation);
Particle.enableEmission = true;
Particle.Play();
//Destroy(effect.gameObject, 3);
Destroy(gameObject);
Particle.enableEmission = false;
}
}
You shouldn't destroy the object the emitter is attached to, then it cannot emit. Disable Coin renderer, disable the script and destroy it after emission (like the line you commented out)
so remove
Destroy(gameObject);
and bring in
Destroy(effect.gameObject, 3);
How would I go about getting the coin to emit the particles while still disappearing when the ball collides with it? Would it be a better idea to put the particles on the ball ins$$anonymous$$d? Sorry about all of the questions. I'm new, and I'm trying to figure out how the things work as well as to get this to work.
Answer by Digital-Phantom · Mar 27, 2015 at 06:29 PM
Make an empty game object, call it effectHolder. Put your particle emitter on this object.
Then when your collision happens instantiate this object at the same position and rotation of your coin.
Now you can fade out/destroy the coin (whichever you want) and the effect will still run. (the coin will be gone but the effect will still be there)
To tidy things up and save memory just add a destroy over time script to the effectHolder and set it to destroy about half a second after the effect has finished.
Simple and effective.
:)
Answer by Priyanshu · Mar 27, 2015 at 06:30 PM
As @hexagonius suggested. Particles don't emit because you destroy the coin on which it is attached.
Either:
Instantiate Effect in this script.
Destroy GameObject in this script.
Add another script to 'Effect Object' which destroys it after some seconds. Put destroy method in its start.
Or
Instead of instantiating 'Effect'. Enable particle on Coin.
Disable Coin Renderer. Which will give a feel of it being destroyed to the user.
Destroy the Coin after some seconds from the same script. Which will destroy particle effect too.
Thank you as well. I'll be sure to keep those in $$anonymous$$d during future projects! ^.^
Your answer
Follow this Question
Related Questions
How to get Zerg Tentacle-like effect (see example in post)? 1 Answer
Check if a pre-determinated area has particles in it? 0 Answers
OnParticleCollision equivalent as a trigger 0 Answers
How to make OnParticleCollision affect multiple GameObject 1 Answer
Instantiating a prefab facing away from it's collision point? 1 Answer