- Home /
Question by
HasnainKhan · Apr 09, 2019 at 06:55 AM ·
physicsnavmeshnavmeshagent
Navmeshpathstatus is not working.
Hello, I am stuck in the middle of my project. I have multiple doors and agent has to pick the nearest one. But when the agent is moving to the nearest door than something (hurdle/obstacle) came up between him and the door. So now agent has to retreat and pick up the nearest door except the one that is already picked. So I completed the nearest door pick task but the hurdle thing is not working as I want. Please help me.
IEnumerator FindNearestDoor()
{
while(true){
yield return new WaitForSeconds(0.1f);
float MaxDistance = Mathf.Infinity;
Door[] AllDoors = FindObjectsOfType<Door>();
foreach (Door door in AllDoors)
{
float DistanceToDoor = (door.transform.position - transform.position).sqrMagnitude;
if (CalculateNewPath(door.transform))
{
Debug.Log("Path is legal");
if (DistanceToDoor < MaxDistance)
{
MaxDistance = DistanceToDoor;
ClosestDoor = door;
_destination = ClosestDoor.transform;
SetDestination();
}
}
else
{
yield return null;
Debug.Log("There is no path");
}
}
}
}
bool CalculateNewPath(Transform door)
{
_navMeshagent.CalculatePath(door.position, path);
if (path.status != NavMeshPathStatus.PathPartial)
{
return false;
}
else
{
return true;
}
}
Comment