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. 78023-lodka.jpg

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!

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;
	}