Recalculating Path question

Why after recalculating path our Enemy doesn’t start from the beginning of the path, but contonues to move from current position. If I understand it correct, for(int… ) loop counts i (for example it start to recalculate path when Enemy moves to Tile i =11) so after RecalculatingPath() Enemy continues moving to the Tile i = 11 where ever it is now, but not starts again from i = 0?

IEnumerator FollowPath(){
        for(int i = 0; i < path.Count; i++)
        {
            Vector3 startPosition = transform.position;
            Vector3 endPosition = gridManager.GetPositionFromCoordinates(path[i].coordinates);
            float travelPercent = 0f;

            transform.LookAt(endPosition);

            while(travelPercent < 1f) {
                travelPercent +=Time.deltaTime * moveSpeed;
                transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
                yield return new WaitForEndOfFrame();
            }
            
        }
        FinishPath();
    }

Hi,

That’s an excellent question, and the reason are these two lines of code in the RecalculatePath method of the EnemyMover class:

coordinates = gridManager.GetCoordinatesFromPosition(transform.position);
path = pathfinder.GetNewPath(coordinates);

In the GetNewPath method in the Pathfinder object, we call BreadthFirstSearch(coordinates); with the passed on coordinates, which are the starting coordinates of/for the enemy on the new individual path.

And the returned new path for these coordinates get assigned to path in the EnemyMover object:

path = pathfinder.GetNewPath(coordinates);

We use this path in the for-loop.

Did this clear it up for you? :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms