Moving an object constantly between two points with a time delay inbetween

Hi,

I’m currently freaking out about an issue:

I want to make a moving obstacle for my infinite Runner, which shall just constantly move between two positions and each time it reaches one position, it shall wait for a short moment before it goes back. (similar to a laserbeam which switches on and off in a specific interval, the object shall move in a specific interval)

I tried for hours to get Vector3.Lerp to work but it doesn’t and I couldn’t find any sufficient solution here on answers, although there are a lot similar questions to it.

Maybe someone could tell me what I’m missing, because this should be a really simple task but it’s driving me crazy right now…

here is the “latest” code I tried, which did not work:

using UnityEngine;
using System.Collections;

public class MovingObstacle : MonoBehaviour {

	private Vector3 obstaclePosition;

	public bool flyingObstacle;
	public float movingSpeed;
	public float interval = 0.5f;
	public float xMax;
	public float xMin;
	public float yMax;
	public float yMin;
	private bool onPosA = true;

	private Vector3 posA;
	private Vector3 posB;

	private float xStart;
	private float xEnd;

	void Start ()
	{
		Debug.Log ("SPAWNED!!");

		SetPositions ();

	}

	void FixedUpdate()
	{

	}

	void Update()
	{
		SwitchPositions ();
	}

	void SetPositions ()
	{
		xStart = obstaclePosition.x + Random.Range (xMin, xMax);
		xEnd = obstaclePosition.x + Random.Range (xMin, xMax);

		posA = new Vector3 (xStart, yMin, 0);
		posB = new Vector3 (xEnd, yMax, 0);
	}

	void SwitchPositions ()
	{
		if (onPosA == true)
		{
			transform.position = Vector3.Lerp (posA,  posB, 1);

			if(transform.position == posB)
			{
			onPosA = false;
			}
		}
		if (onPosA == false)
		{
			transform.position = Vector3.Lerp (posB,  posA, 1);
			if(transform.position == posA)
			{
				onPosA = true;
			}
		}
	}
}

Thanks in advance!

Best regards

This is a simple delay system using Time.time:

private float waitamount = 2f;//in seconds
private float finishedmovingtime = -waitamount; //this comes to a flat 0. So it can start without waiting for waitamount

void SwitchPositions ()
{
if(finishedmovingtime + waitamount < Time.time) //if it took enough time since last movement...
{
if (onPosA == true)
  {
  transform.position = Vector3.Lerp (posA, posB, 1);
  if(transform.position == posB)
    {
    onPosA = false;
    finishedmovingtime = Time.time;
    }
  }
  if (onPosA == false)
  {
  transform.position = Vector3.Lerp (posB, posA, 1);
  if(transform.position == posA)
    {
    onPosA = true;
    finishedmovingtime = Time.time;
    }
  }
}
}

First, take a look at the documentation for the lerp function : Unity - Scripting API: Vector3.Lerp.

You’ll see that it takes two Vector3 and a percentage. 0 means the first, 1 the second and .5 the middle.

Now, there is many ways to achieve what you want, here is one using coroutines :

private IEnumerator Start()
{
	// Loops each cycles
	while( Application.isPlaying )
	{
		// First step, travel from A to B
		float counter = 0f;
		while( counter < travelDuration )
		{
			transform.position = Vector3.Lerp (posA, posB, counter / travelDuration);
			counter += Time.deltaTime;
			yield return null;
		}

		// Make sure you're exactly at B, in case the counter 
		// wasn't precisely equal to travelDuration at the end
		transform.position = posB;
		
		// Second step, wait
		yield return new WaitForSeconds(waitDuration);

		// Third step, travel back from B to A
		float counter = 0f;
		while( counter < travelDuration )
		{
			transform.position = Vector3.Lerp (posB, posA, counter / travelDuration);
			counter += Time.deltaTime;
			yield return null;
		}

		transform.position = posA;
		
		// Finally, wait
		yield return new WaitForSeconds(waitDuration);
	}
}