Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Move to closest waypoint

Discussion in 'Scripting' started by Aizle, Jan 1, 2021.

  1. Aizle

    Aizle

    Joined:
    Apr 14, 2019
    Posts:
    3
    Hey guys,

    Do anyone of you have a good solution for finding "closest waypoint" ? I don't want it to be a predetermined path or a random path. But whenever I try to do it, it works for about 2 or 3 waypoints, and they start moving like they're stupid. I decided to scrap my crap script yesterday and look for help. I've got health, and all that going. Just want them all to move from a single waypoint script basically.

    • Every red circle is spawning a squad and i want them to start moving on the lane their spawnpoint is on.

    • upload_2021-1-1_13-33-8.png
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Your title and description dont really match. To get the closest waypoint, have a list of all waypoints you did not yet visit, and all you did visit, then get the closest from the unvisited list, go there, and move it to the visited list. Repeat.

    However since you wrote you want the objects sparning in the red circles to only move on their lanes (which i assume is the green lines between the squares), you could just send a raycast from the object down the lanes (in all viable directions) and go to where the hit.distance is smallest. This only works under the assumption that you cannot miss an object on a lane.
     
    Aizle likes this.
  3. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
    Try NavMesh or Raycast
     
    Aizle likes this.
  4. Aizle

    Aizle

    Joined:
    Apr 14, 2019
    Posts:
    3
    Cheers, now I know where to look :)

    oh, I didnt intend on having a misleading title.

    thanks anyway!
     
    Yoreki likes this.