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
there.’ - 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(Detect_Player_Input.getObjProperties.name).transform; //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!
– UPDATE –
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…