Unity 5: turning particles on/off
Hi,
I checked for this problem in the archive but all answers I could find date back to 3 or 4 years ago and are clearly obsolete. I am using Play() and Stop() to achieve this but the result is really strange. This is what I am doing:
I want the particle system to be off when the scene starts so in Start() I have:
pSystem = this.GetComponent<ParticleSystem>();
pSystem.Stop ();
Then when the player presses Fire1 particles should be activated and should be stopped again when Fire1 button is released. In Update() I wrote this:
if (Input.GetButtonDown ("Fire1"))
pSystem.Play ();
if (Input.GetButtonUp ("Fire1"))
pSystem.Stop ();
What happens is that it works but only once: after particles have been turned on and off once, pSystem.Play() doesn't have any visible effect any more even though it is called again when Fire1 is pressed.
Any idea of what could cause this? Thanks in advance!
Answer by FrigginTommyNoble · Feb 28, 2016 at 01:21 AM
Hey @robdil, so I had the same issue. After much trial and error, it looks like the combination of Play() and enableEmission is what works. Here's a simplified thruster script attached to a particle system, seems like it would apply to your use case.
using UnityEngine;
using System.Collections;
public class ThrusterController : MonoBehaviour {
private ParticleSystem ps;
void Start()
{
ps = GetComponent<ParticleSystem>();
PlayThrusters();
}
void Update()
{
// Thrust button pressed, start particle system
if (Input.GetButtonDown("Thrust Forward"))
{
PlayThrusters();
}
// Thrust button released, deactivate particle system
if (Input.GetButtonUp("Thrust Forward"))
{
StopThrusters();
}
}
void PlayThrusters()
{
ps.Play();
ps.enableEmission = false;
}
void StopThrusters()
{
ps.enableEmission = false;
ps.Stop();
}
}
Hope that helps.
line 30 has a small bug. it should say ps. enableEmission = true; (not false).
unity flags enableEmission as obsolete and the replacement attribute "emmision.enabled" seems to be readonly. so what gives? why do you have to use enableEmission to make it work as expected. thanks for the solution friggintommynoble, but it would be nice if we understood the whys behind it.
Your answer
Follow this Question
Related Questions
How would you make a dust trail with a 2d sprite animation? 0 Answers
Particle System Instantiate wrong transform, position on Rig Builder / ragdoll conflict 0 Answers
Particles syncing on the Unity Network 4 Answers
I have a particle system that is playing at the wrong time... 1 Answer
Having Player light torches Using Particle System and OnCollisionEnter? 1 Answer