# How to get the distance of a path?

Hey,
So i’ve got a situation in which i have a little AI Critter that is meant to run away when the player gets too close.
I’ve got to make it so there are vector3 positions that are ‘safe’ for the critter to hide from the player (in the game it’ll be something like a rabbit running down a hole).
but i want it so that the critter will choose from this list of locations and choose a hiding spot that is both away from the general direction of the player and then the shortest path possible out of those locations left over. So the rabbit would choose the hole that is away from the player and closest to him in that direction.
The problem is i don’t know how to compare the distances of the calculated paths so i can tell which is the shortest route. Any ideas how i can do this?
I’m using nav mesh agent by the way.

first make array of safe_spots (array of Vector3) “safe_spots_array”, add to array all safe spots.
make array of paths (array of NavMeshPath) “navmesh_path_array”.
make array of float “paths_distance_array” for compare length of all possible paths.
make int “index_of_shortest_path_array” for put here calculated index of shortest path of “navmesh_path_array”.
make NavMeshPath “best_path” for store shortest so best path.
Now you can use “for” loop:

``````function Find_best_path()
{
for (var i = 0; i < safe_spots_array.Length; i++)
{
// calculate all posible pathes based on all safe spots
NavMeshAgent.CalculatePath(safe_spots_array<em>, navmesh_path_array*);*</em>
``````

// now set current calculated path to navmesh agent
NavMeshAgent.SetPath(navmesh_path_array*);
_// call stop navmesh agent for provide distance calculation, because SetPath initiate navmesh agent moving*
NavMeshAgent.Stop();
// then calculate remaining distance from navmesh to target safe spot
paths_distance_array = NavMeshAgent.remainingDistance;
}
// then you need to compare values of “paths_distance_array” and choose those array element with smallest value, for this use function “Min_value_index”
index_of_shortest_path_array = Min_value_index(paths_distance_array);
best_path = navmesh_path_array[index_of_shortest_path_array];
//now use “best_path” as actual path for NavMeshAgent
NavMeshAgent.SetPath(best_path);
}_

function Min_value_index (Array_float : float[]) : int {
var min : float = Array_float[0];
var index: int;
for (i = 1; i < Array_float.Length; i++)
{
if (Array_float < min)
{
min = Array_float*;
index = i;
_}
}
return index;
}*

This code needs to be improved but the concept it shows clearly_

Assuming it’s a straight line then try this

``````tempDist = Vector3.Distance(myRabbit.position, EscapePlace.position);
``````

So run through all your “EscapePlaces” (sorry I don’t know how you reference them).

Set another float “nearest” and before you run though it set that to something high
e.g. nearest = 1000f;

then get the result from that Vector3.Distance and if tempDist < nearest set the nearest to = tempDist and recorde the transform information for that object in another GameObject or Transform (depends what information you want)

By the end you’ll have the closest distance held in nearest and the position stored in whatever you use to store that GameObjec/Transform.

Oh and to get one that’s away from your player I’ve seen people use lookat and reverse it but not tried that myself so I’ll need little while to code it.

EDIT:

faster to find than I thought!

``````transform.rotation = Quarternion.LookRotation(traansform.position - Player.trasform.position)
``````

Not actually got access to unity at the moment so sorry it’s not all coded.

EDIT 2:

OK, again not got access to Unity at the moment but I believe you can get the remaining distance for a path using the NavMeshAgent worth looking into at least.

``````GetComponent<NavMeshAgent>().remainingDistance;
``````