Support Forum

Determine nearest object via pathing

I’ve been digging through the documentation and can’t find a way to determine the nearest object based on the shortest path. An object in my game needs to scan a list of about 100 potential targets and select the shortest path. I know how to find the closest object based on distance alone, but with obstacles and other things in my game that isn’t as useful as being able to find the shortest path.

Any guidance will be appreciated! Thanks in advance.

Hi

Take a look at https://arongranberg.com/astar/docs/multitargetpath.html

Note that if you want to use one of the built-in movement scripts you will want to use ai.SetPath(MultiTargetPath.Construct(...)), not seeker.StartMultiTargetPath directly.

1 Like

Fantastic! I recently upgraded to the pro version so I’ve got access to this now :grin:

Though I was wondering, is it possible to call seeker.StartMultiTargetPath on a list rather than an array? There are objects constantly being added/removed to a global list of enemies that I would love to be able to use this method on but I’m having to use .ToArray() every time.

I can totally refactor to make this work as I’m sure it’s more efficient than what I’m currently doing but I also wanted to check and see if there’s something I’m overlooking.

You would want to make a copy anyway. The path calculation is done on a separate thread and you really don’t want the pathfinding code to be reading from a list at the same time that you might be modifying it.

1 Like

Hey, @aron_granberg I just deleted my previous post that was full of nonsensical questions and wanted to apologize. I’m a hobby dev who doesn’t have a lot of time to spend on my passion project. When I bought the pro version of A* I expected it to be much more plug-and-play than it is so I asked a lot of dumb questions on the forums.

Yesterday I spent a few hours really reading through the docs and picking apart the code in the package and the example scenes and holy smokes: I understand it so much better now. Sorry for not doing the work on my end before asking questions. I really appreciate the product you’ve built!

1 Like