Calculate path distance between object and agent?

I have 2 objects. One is the player (a navmesh Agent) and the other one is a static object.

I want to calculate the distance between the 2 objects and:

  • if Im too far away but I can get to
    the object - get there
  • if Im too far way and can’t get to the object - say ‘I cant get
  • if Im close enough to the object - say ‘Im there.’

This is what I have so far:

	private var mainPlayer : Transform ;											
	private var objTrigger : Transform;
	private var distanceToObject : float ;
	private var passableMask = NavMesh.GetNavMeshLayerFromName("Default");		//< ignore everything but "Default"
	private var possiblePath : boolean;		
	private var newNamMeshPath : NavMeshPath;
	private var hitDistance : NavMeshHit;

function Update (){
	mainPlayer = GameObject.Find("PLAYER").transform;										//gets the position of the player
	objTrigger = GameObject.Find(;		//gets the position of the selected object.
 	distanceToObject = hitDistance.distance;
	possiblePath = NavMesh.CalculatePath(mainPlayer.position, objTrigger.position, 1, newNamMeshPath );	//calculates a path between the mainPlayer and the objectTrigger

function LookAt (){
	if (distanceToObject >= 3 && possiblePath == true) {						//if I'm too far away but I can get to the object - get there
		GetComponent(NavMeshAgent).destination = objTrigger.position;
		if (distanceToObject >= 3 && possiblePath == false) {					//if I'm too far way and can't get to the object - say it to the player
		//Feed to GUI_Layout the objectTrigger "CannotReachDestination" string
	else if (distanceToObject < 3) {											//if I'm close enough to it then Look at it.
		//Feed to GUI_Layout the objectTrigger "LookAt" string

I can get the mainPlayer and objTrigger well, everything else not really…

I’m trying to use the NavMesh.CalculatePath to tell me if a path is possible between the 2 objects but I think I’m using it incorrectly.

I’m trying to use the NavMeshHit.distance to tell me if the distance is greater or smaller than ‘x’ but I don’t understand how to use it either.

I looked at the documentaton but I can’t figure it out. Any help would be very appreciated!


Using the remainingDistance (with the pathPending) still doesn’t seem to work but I can see its the right direction to go.

This is what I’m trying to do so that the ‘readyToOpenContainer’ becomes true when the player ends the path:

if (GetComponent(NavMeshAgent).pathPending == false && remainingDistance <= 0.1 ){
			readyToOpenContainer = true;

The problem is that the moment he starts to calculate the path I get a remainingDistance = 0, and once the path is calculated I get a remainingDistance = 2.457562E-07…

NavMesh.CalculatePath() doesn’t seem to work how I think it does either. So I’m no longer using it in my code. However, if you want to get the distance between the NavMeshAgent and the object, you can simply use GetComponent(NavMeshAgent).remainingDistance; (I’m not sure if this is the exact syntax as I use C# but the remainingDistance property is what you’re looking for I think). One thing to look out for, is if the path is not yet calculated (e.g. the agent doesn’t know how far away it is), then it’ll return Math.Infinity. You can use NavMeshAgent.pathPending to find out if it’s figured out the path yet.

By the way, you are using hitDistance.distance; in your code, but nowhere you’re specifying what that value is.

tried to fix using this space for a question. Added the good reply from justinl here:

Thanks for the extra info. I wouldn’t use the NavMeshAgent in your situation because it’s too unreliable with calculating the path etc. Especially if you’re calculating it at the same time as everything else in the world. You can set a distance on a Raycast so that it only reaches out 1 unit in front of the player and also, if the Raycast hits a wall, you can detect that he’s standing in front of a wall and cannot even see the fridge on the other side. The player doesn’t need to know the path to get to a fridge, to know if he/she is standing in front of it. Is the user interaction like Diablo/Warcraft? (Where you click on an object and the computer moves the player to it?)

This worked for me.