• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DiamondRBX · May 10 at 12:58 PM · unity 2daipathfindinglistsmovetowards

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.alt text

screen-shot-2022-05-10-at-85719-am.png (496.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiamondRBX · May 11 at 11:55 AM 0
Share

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

screen-shot-2022-05-11-at-75446-am-min.png (139.8 kB)
avatar image sodacookiedev DiamondRBX · May 11 at 12:03 PM 0
Share

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);

avatar image DiamondRBX sodacookiedev · May 11 at 12:55 PM 0
Share

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

alt text

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

235 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Have falling object exit from a collider after collision? 2 Answers

Why do the enemies not follow me? 1 Answer

NavAgents use way too many resources? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges