Method to find nearest object to a node?

I’m sure I can throw something together fairly easily, but I figured I’d ask if there is a method that does this already.

Basically (instead of finding the nearest node to an object, that’s easy and not what I want), I want to find the nearest GameObject to a specific node. This is for a JobDispatch class, that hands out jobs to available NPCs. I don’t want NPCs being selected from across the map if I can avoid it. Of course, I’m not talking about near in terms of Vector3.Distance, I’m talking about pathfinding costs.

EDIT: trying to do something like this (this is returning null, however, and I know there are available agents):

        AgentBehavior availableAgent = null;
        uint minPathCost = uint.MaxValue;

        foreach (var agent in _allAvailableAgents)
            if (!agent.IsAvailable) { continue; }

            ABPath newPath = ABPath.Construct(agent.transform.position, position);
            if (minPathCost < newPath.cost)
                minPathCost = newPath.cost;
                availableAgent = agent;
        return availableAgent;

Is there any such method?


You may be interested in MultiTargetPath - A* Pathfinding Project

That looks promising, something like this could work:

       Vector3[] allPaths = new Vector3[_allAvailableAgents.Count];
        for (int i = 0; i < _allAvailableAgents.Count; i++)
            allPaths[i] = _allAvailableAgents[i].transform.position;
        Path path = _seeker.StartMultiTargetPath((Vector3)position, allPaths, false, ParseMultiPath);

However, is there a built-in way to find out which path in the parameter allPaths it took (eg, which index)? Otherwise I think it’ll be better to just iterate over all my agents and use StartPath until I find the shortest (because then I’ll also have the associated agent, which is what I really want).

In any case it looks like I need to use Seeker.StartPath instead of just ABPath.Construct, so that’s helpful. Thank you.

That’s not necessary. You acn use MultiTargetPath.Construct together with AstarPath.StartPath instead of using a Seeker.

Yes, the path has the chosenTarget field. See MultiTargetPath - A* Pathfinding Project

Yes, the path has the chosenTarget field. See MultiTargetPath - A* Pathfinding Project

Ahh, didn’t see that, thank you so much!