I have a list whose elements are Vector2
points in world space:
(9, -9)
(9, -13)
(5, -13)
What I want is that my enemy character should first go to (9, -9) position, after reaching that target, go to (9, -13) position, and after reaching there, move to (5, -13).
I tried this in the script below. I gave the first element of the list to MoveTowards
as a target, but before my character could reach its target, the next element of the list was given as a target.
How can I do this without using the for-loop? Or how can I temporarily stop the for-loop until my character reaches its destination?
public Rigidbody2D rb;
private float speed = 2f;
public Vector2 target;
public NewNavmesh pathTarget;
private void Update()
{
for (int i = 0; i < pathTarget.ways.Count; i++)
{
target = pathTarget.ways[i]; //Vector2 paths in new list
transform.position = Vector2.MoveTowards(this.transform.position, target, speed * Time.deltaTime);
int indexInList = pathTarget.ways.IndexOf(pathTarget.ways[i]);
pathTarget.ways.RemoveAt(indexInList);
}
}