Move from Point A to Point B along Path

This is frustrating me to no end. I have a player which needs to move along a path (not necessarily straight). The path is a List positions. I need the player to move to position 1, then 2, then 3, etc and stop at the last point. This sounds easy enough but I can’t seem to get it to work. Here’s what I’ve tried:

Method A:

``````        public IEnumerator MoveOverSeconds(GameObject objectToMove, Vector3 end, Vector3 next)
{
float elapsedTime = 0;
Vector3 startingPos = objectToMove.transform.position;
var seconds = Vector3.Distance(startingPos, next) * 0.3f;

while (elapsedTime < seconds)
{
objectToMove.transform.position = Vector3.Lerp(startingPos, end, (elapsedTime / seconds));
objectToMove.transform.LookAt(next);

elapsedTime += Time.deltaTime;

yield return new WaitForEndOfFrame();
}

objectToMove.transform.position = end;
}
``````

The array of positions passes in array[0] as “end” and array[1] as “next” IF there is an array[1]. After this is called as a StartCoroutine I call movementlist.RemoveAt(0). So, basically I’m trying to get to the first position then I remove it from the array and call this again with the array[0] now being the next position. Results are close with this method… except the player walks directly to the end position, skipping all the stops in between.

Next I tried a different Coroutine:

``````        private IEnumerator MoveAlongPath(GameObject objectToMove)
{
if (waypoints == null || !waypoints.Any())
yield return null;

float distanceThreshold = 0.05f;
Vector3 startingPos = objectToMove.transform.position;
if (waypoints.Count > 1)
objectToMove.transform.LookAt(waypoints[1]);

while (Vector3.Distance(objectToMove.transform.position, waypoints[0]) > distanceThreshold)
{
//objectToMove.transform.position += objectToMove.transform.forward * 1f * Time.deltaTime;
objectToMove.transform.position = Vector3.Lerp(startingPos, waypoints[0], 0.3f);
yield return null;
}

objectToMove.transform.position = waypoints[0];
waypoints.RemoveAt(0);
yield return new WaitForSeconds(2f);
}
``````

I can’t even describe the results of this one… my player basically turns completely horizontal and starts moonwalking seemingly randomly around the screen.

I’m sure I’m close, but if someone could point me in the right direction please let me know. Thanks!

EDIT: I forgot to mention that this is in 3 dimensions. I just store the positions as Vector2 because the Y value is always 0f in this scenario.

Method A looks fine at a cursory glance.
I suspect you aren’t waiting for long enough before starting the next MoveOverSeconds with new parameters
Cant you just wrap the whole method A body into another loop?
eg.

``````            public IEnumerator MoveOverSeconds(GameObject objectToMove, List<Vector3> list )
{
while (list.count>2){
//method A body, using list[0] and list[1]
//removeAt(0)
}
}
``````

I was thinking the same thing. I just added:

if (updateMoveLocked)
return;

to my update call. I set this to true immediately after this check and set it to false after one full move from within my coroutine. This causes the MoveOverSeconds to “wait” until the first is finished. This gets me about 99% there. A bit of smoothing and I think this issue is resolved. Thanks for the idea hpjohn, and hopefully this helps someone else

You probably want to yield return null instead of WaitForEndOfFrame. WaitForEndOfFrame is designed to run in the same frame but after the GUI is rendered. Yielding null will wait until the normal execution time in the next frame.

This has a chart showing when everything happens: