Coroutine not running to end?

I am moving an object in Unity3d from point A (the objects current location) to point b (a public Vector3 added through the Inspector.

I am using Lerp to handle the movement of the object, and when I place my code under “Update” it works exactly as planned. But I need to make this triggerable (when the number of touches >= 10, this movement is triggered).

Coroutines seem to be what I need, and the movement fires as expected, but it isn’t running to completion. The object moves a tiny bit, then stops.

Could someone point me at my error? I feel like the answer is in the yield statement, but I’m not sure what it should say.

Thanks in advance! Code below.


	void Update()
	{

			limitText.text = tapLimit.ToString();
			if (Input.GetMouseButtonUp(0))
		{
			tapCount = int.Parse(tapText.text);
            tapCount += 1;
            tapText.text = tapCount.ToString();
			currentTaps = tapCount;

			{
				if(tapCount >= tapLimit)
					StartCoroutine(EndLevel()); 
				
        	}
		}
	
	}

	IEnumerator EndLevel()
		{
			EndScreen.transform.position = Vector3.Lerp(EndScreen.transform.position, pointB, _t);
			_t += Time.deltaTime/2;
			yield return null;
		}

You need to place it in a loop to repeat the action.

IEnumerator EndLevel(){
     EndScreen.transform.position = Vector3.Lerp(EndScreen.transform.position, pointB, _t);
     _t += Time.deltaTime/2;
     yield return null;
}

This will run once until the yield then next time it will start from the where it left off, the yield. But there is nothing after that so it returns.

What you need is to loop until a condition is met:

IEnumerator EndLevel(){
   while((_t < 1){
     EndScreen.transform.position = Vector3.Lerp(EndScreen.transform.position, pointB, _t);
     _t += Time.deltaTime/2;
     yield return null;
   }
 }

Now as long as _t is smaller than 1, it is looping. Now you are maybe not using the best way but at least that should get you going.

Maybe you want to use MoveTowards instead which is easier to control.