- Home /
how to make particles start with smaller emission rate and than keep it constant while holding a key
Hello, I'm making 2D pirate ship game and having some trouble with particle system. This is how it looks like when moving right now. 
I would like the particle system to emit less particles when ship is just starting to move and when it slowly stops. So emission rate is not constant all te time the ship is moving.
I'm using this script:
private ParticleSystem trail;
void Start ()
{
trail = GetComponent<ParticleSystem>();
}
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
trail.Play();
}
else
{
trail.Stop();
}
}
}
As you can see particle system is only played when i hold UpArrow key. I'm also curious if it's possible to emit particles while the ship is still moving and stopping slowly after i already released the key. Is there any method or smthg that says that this particle system will play simply when the object it's attached to is moving?
Thank you very much for answers!
Answer by iabulko · Sep 12, 2016 at 04:07 PM
You can easily check if object is moving by checking the difference between it's actual position and position from last frame. You can also make emission rate dependent from it's speed using that difference, for example:
private ParticleSystem trail;
public GameObject ship;
private Vector3 lastPosition;
void Start ()
{
trail = GetComponent<ParticleSystem>();
lastPosition = ship.transform.position;
}
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
trail.Play();
}
else
{
trail.Stop();
}
float speed = Vector3.Distance (ship.transform.position, lastPosition);
var em = trail.emission;
var rate = new ParticleSystem.MinMaxCurve();
rate.constantMax = speed;
em.rate = rate;
lastPosition = ship.transform.position;
}
thank you very much @iabulko for your help but the script doesnt seem to work for me. I'm probably doing smthg wrong but after using your example nothing actually changed, the ship is still emitting particles only when I hold uparrow key. I have emission rate set to 66 in the inspector should I change it to curve ins$$anonymous$$d or I totally don't get the point? :D
Your answer
Follow this Question
Related Questions
Particle emission : Give each particle a specific direction 1 Answer
Particle System not working properly on Android 0 Answers
Emit 1 particle per second 1 Answer
Particle system does not re-emit until Start Lifetime elapsed 4 Answers
Particle System Still Emitting Particles Even If Emission Is 0 0 Answers