- Home /
Particle system that follows player and turns on or off
Hello all,
I am trying to get some particle effects on the main character of my game. There are 2 different ways that I have tried this and I have run into problems with both. Perhaps someone can help me come up with a better method or improve one of my existing ones?
What I'm trying to do is quite simply actually. Whenever I press a button, particles should emit from the player's body, but here's the catch: they should follow him when he moves. Here are the two ways I have tried, and the problems with each one:
Attach a particle system prefab as a child to the player.
After I have attached it to the player, I do not know how i can get it to "turn on" or "turn off" in the scripts. I have tried using:
sprayer.particleEmitter.particleSystem.Play();
But this results in an error that says that I do not have a particle emitter attached. I don't know how I am supposed to access the particle system otherwise because although I have declared sprayer as a particle system, intellisense does not give me any options that have to do with the particlesystem.
Simply instantiate the particle system at the player's position.
The reason that this fails is because the particle system stays in the place that it was instantiated. I have tried attaching a script to the system that sets its position to the player's transform.position. But this does not work because prefabs cannot reference an object that is inside the scene already.
Recently, I am also getting an error similar to this one: http://answers.unity3d.com/questions/238274/updateparticle-system-should-not-happen-on-disable.html
Any other advice on playing around with particle systems would be greatly appreciated. Thanks in advance!
Answer by drizztmainsword · Jun 08, 2012 at 04:22 AM
So when Unity update to 3.5, they implemented a new particle system (Shuriken). The new particle system is used with the ParticleSystem component. ParticleEmitter is part of the old particle system.
If you have a Particle System component on your "sprayer" object, you're going to need a few variables.
First, you're going to want a variable (type of ParticleSystem) for the particle system, which you can assign in the editor by dragging the system into the appropriate place or you can grab in code using the GetComponent() function.
Then, once you have that variable, you should be able to call the Play() and Stop() functions normally, and intellisense should be able to see them.
Thanks very much for the info. This has solved my problem with ease!
Your answer
![](https://koobas.hobune.stream/wayback/20220613072519im_/https://answers.unity.com/themes/thub/images/avi.jpg)