Move a object and then return , like loop

Hi Guys, I was wondering how can i move an enemy to the left and to the right in a short space, like an infinite loop.

I’m trying to think in a good method, but all that i’ve got until now is this

void Move()
	{
		time= Time.deltaTime;
		if (time<= timePosition) 
		{
			transform.Translate(-speed*Time.deltaTime,0,0);
		}
		if (time > timePosition) 
		{
			transform.Translate(speed*Time.deltaTime,0,0);
		}
		
	}

basically it’s a repeated movement applied in the update.

You do this in a single line using Mathf.PingPong.

void Update() {
    transform.position = new Vector3(Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z);
}

I maybe late but I would recommend using a coroutine rather than Update.
Then by doing something like this…

IEnumerator Move(){
     bool goingLeft = true;
     transform.Translate(-speed*Time.deltaTime,0,0); 
     while(true) {
          yield return new WaitforSevonds(timePosition);
          goingLeft ? transform.Translate(speed*Time.deltaTime,0,0) : transform.Translate(speed*Time.deltaTime,0,0);
          goingLeft ? false; true;
     }
}