-
Int3.costMagnitude is basically the distance. It is named costMagnitude because that is the default cost if there is a direct connection between those two nodes. Usually there isn’t. So the script posted in the first part will list the euclidean distances to the different nodes, not the actual number of nodes it took to get there.
If you want the farthest node, as in the number of nodes it takes to get there:
// Replace GraphNode with Node if you are not using the beta List<GraphNode> availNodes = PathUtilities.GetReachableNodes(AstarPath.active.GetNearest(transform.position).node); GraphNode furthestAway = availNodes[availNodes.Count-1];
If you want it including stuff like node weights or taking into account different connection costs between different nodes (like if it is not a grid graph):
ConstantPath p = ConstantPath.Construct (transform.position, 99999999, null); AstarPath.active.StartPath (p); // If you use the beta, you can use AstarPath.active.StartPath (p, true) to bypass the path queue and calculate it directly AstarPath.WaitForPath(p); GraphNode furthestAway = p.nodes[p.nodes.Count-1];
Note that prior to the beta, the ConstantPath approach might actually return node arrays with slightly unsorted nodes, I do not think it should affect the furthest away node, but I am not totally sure.
However, thinking about it, I think the best approach is actually to use the RandomPath (as counterintuitive as it sounds). Because it only stops when it has reached a minimum G-score, if we set that G-score so high that it will never reach it, it will stop at the furthest away node it can find.
RandomPath p = RandomPath.Construct (transform.position, 99999999, null); AstarPath.active.StartPath (p); // If you use the beta, you can use AstarPath.active.StartPath (p, true) to bypass the path queue and calculate it directly AstarPath.WaitForPath(p); GraphNode furthestAway = p.nodes[p.nodes.Count-1];
Looks similar to the ConstantPath approach, but the constant path has to store ALL the nodes it visits, the RandomPath only has to store the path to the furthest away node. So this saves memory and is faster.