- Home /
How do I moveTowards all the items in a list one at a time?
So I am trying to build a pathfinding AI for my Coding class. I have a list that contains all the best X and Y coordinates that the AI found. I am trying to run a bit of code that procedurally takes item i of the list, moves the object towards the coordinates until it has reached those cords, then does it again until it has done the whole list. For some reason though, it gets stuck and never moves towards any of the coordinates.
Answer by sodacookiedev · May 10 at 04:25 PM
Hi, there are some problems in your code.
1) When you call Vector2.MoveTowards, the function will not change your position (which is the first parameter). It will calculate a new Vector that is the position + some movement towards the target. You probably want something like this:
transform.position = Vector2.MoveTowards(transform.position, _targetPos, 50f);
2) You use the "WaitUntil" and compare with a position. The problem is, your loop will wait until the position is at the target, but that will not be the case after just one call. Instead you should use something like "WaitForNextFrame", which will make your do one move each frame.
3) Comparing a position (which will be a float) is problematic, since for example 2.0001 is not equal to 2. Instead you should try somehing like "Mathf.Abs(transform.x - _targetPos.x) < 0.1" for example.
Hi, thanks for the response! Im not sure if I implemented your code correctly, it for some reason teleports instead of lerping (I tried it with both Lerp and MoveTowards), and for some reason it only does this until it reaches i=2, then it gets stuck. Do you think you could help?
![alt text][1] [1]: /storage/temp/196201-screen-shot-2022-05-11-at-75446-am-min.png
I think a problem lies with the lerp function. If you give 1 for t (the third parameter) it will always return the target position. (see https://docs.unity3d.com/ScriptReference/Vector2.Lerp.html)
Instead you could use 0.1f or a similar value. However this will have the side effect, that the speed of your movement scales with the distance. So it will become slower the nearer it is.
If you want normal movement, MoveTowards was the correct approach. You probably want the change the distance (100f currently) into something smaller and possibly multiply it with Time.deltaTime to get proper movement speed. so:
transform.position = Vector2.MoveTowards(transform.position, _targetPos, 1*Time.deltaTime);
Hmm, I tried adding that but it kept teleporting back to start. I added an if statement to stop that but now it just teleports to the first position and I cant find out why. Also, thanks for the help C:
A link to what I see: https://www.youtube.com/shorts/3iGXtKpHnvk
Your answer
![](https://koobas.hobune.stream/wayback/20220613060401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A* Pathfinding Enemy AI, How to search for something else when no path to player available 0 Answers
Set AI Destination Setter target from Script 2 Answers
Sidescroller AI/Pathfinding/Flags/Waypoints 0 Answers
Modify NavMeshPath for Tactical FPS (slicing the pie/sweeping maneuver) 0 Answers
How to do AI pathfinding on trees and other vertical surfaces? 0 Answers