how to compare position.x last position to current?

Hi, I wanted to compare the last position with the current position of my player.
Here’s my current code:

	public GameObject player;

	Vector2 lastPos;
	Vector2 currentPos;
	

	void Update(){

		currentPos = player.transform.position;

		if (currentPos.x < lastPos.x) {
			gameObject.GetComponent<ParticleSystem> ().enableEmission = true;
		} else {
			gameObject.GetComponent<ParticleSystem> ().enableEmission = false;
		}

		lastPos = currentPos;

	}

I’m trying to play the particle system whenever the current position of the player is lower than before(the lastpos). I’m not sure what’s wrong though, my logic or code or both? any help would be appreciated. Thanks!

Ok I figure out what is your problem.

You must enable your parcticle system for a minimum amount of time. actualy you active and desactive the emiter imediatly after the movement. This script works on my new project :

	public GameObject player;
	
	Vector3 lastPos;
	Vector3 currentPos;
	float _time = 0;

	void Awake() {
		lastPos = player.transform.position;
	}
	
	void Update(){
		
		currentPos = player.transform.position;



		if (currentPos.x < lastPos.x) {
			gameObject.GetComponent<ParticleSystem> ().enableEmission = true;
		} else if (_time > 2f) {
			gameObject.GetComponent<ParticleSystem> ().enableEmission = false;
			_time = 0;
		} else if (_time < 2f) {
			_time += Time.deltaTime;
		}
		
		lastPos = currentPos;
		
	}

I hope this help.