Length of a theoretical path - how do i find it?

I got an AI which i am working on getting to run to cover - but right now it’s taking the exact distance between it and the cover, as the distance. That’s pretty problematic, as it will run all the way around a wall to get to a point - how do i get the length of the shortest route between the AI and a point without it actually trying to move it, and still being able to use the it as a float(or int, double… what ever)?

Really need this to progress right now :slight_smile:




You need to calculate the path in some way. Here is some code to do it quick and dirty.

public void GetDistance ( Vector3 startPosition, Vector3 endPosition ) { ABPath p = ABPath.Construct ( startPosition, endPosition, null ); AstarPath.StartPath (p); // Force the path to be calculated right now // Might not be optimal if you want a stable frame rate AstarPath.WaitForPath (p); return p.GetTotalLength (); }

Also, if you want to find e.g the nearest cover, you might want to use the MultiTargetPath instead, see http://arongranberg.com/astar/docs/class_pathfinding_1_1_multi_target_path.php

And from the master him self - i’m honered - truely a genius in the eyes of this still young and stupid programmer…! Thanks for the quick answer - sorry for the double post though - and thanks for the free to use pathfinder - i’ll without a doubt buy it when i get in a position where i can trow money after gamedev stuff!

I’ll try it!

Okay - sorry for noobing all out here but:
I am getting which states that “ABPath” can’t be found - what i am having is a static class with the piece you wrote in it - but i am getting an error at (what is in the part you wrote) the second line… What am i doing wrong… how would you do it? This static class is all a seperate class which is called from my AI which is why i made it static…


try to add
using Pathfinding;
at the top of your script.

oh - thanks :slight_smile: it work! :slight_smile: