How to check if agent destination can be reached?

Hello!
So I have this problem with my AI. I made all doors as an dynamic obstacles, so that the enemy couldn’t go through the door object. But the problem is that if for example the enemy is chasing the player and the player closes the door, the enemy starts running in one spot, he doesn’t go to find the next possible way to reach the player. And the other problem is that if the player closes the door and the enemy is trapped for example in some kind of room, he just starts walking in one spot. He doesn’t do anything… Maybe there a solution to check if the destination to the player is possible and if not than do something like for example start patrolling the area going to the possible waypoint. Or if there is no possible waypoint than just stand, idle?

The enemy script:

using UnityEngine;
using System.Collections;

public class chase : MonoBehaviour {

public Transform player;
public Transform head;
public float DistanceA;
public AudioClip[] footsteps;
Animator anim;

public float C_Pitch;
public float S_Pitch;

public GameObject HellHound;

public float canPlayTime_Patrol = 0.3f;
public float canPlayTime_Chase = 0.3f;

private NavMeshAgent agent;

public static bool chasingPlayer = false;
public bool isPatroling = false;
public static bool isSeeking = false;
private bool Stand = false;
private bool cantplay = false;

string state = "patrol";
public Transform[] waypoints;
int currentWP = 0;
public float rotSpeed = 0.2f;
public float rotSpeedSeek = 1.5f;
public float rotSpeedPatrol = 0.2f;
public float speed = 1.5f;
float accuracyWP = 5.0f;
public float attackDistance;
public float chargeDistance;
public float spotDistance;
public float seekDistance;

public Transform CurrentWP;

void Awake ()
{
	chasingPlayer = false;
	isSeeking = false;
}

// Use this for initialization
void Start () 
{
	anim = GetComponent<Animator>();
	agent = GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update () 
{
	Vector3 direction = player.position - this.transform.position;
	DistanceA = Vector3.Distance(player.position, transform.position);
	direction.y = 0;
	float angle = Vector3.Angle(direction, head.forward);
	CurrentWP = waypoints [currentWP];
	RaycastHit hit;

	if (DistanceA > spotDistance) // if the player is far away
	{
		state = "patrol";
		chasingPlayer = false;
		Player_is_Moving.chasing = false;
		isSeeking = false;
	}

	else if (DistanceA < attackDistance && angle < 30 && DistanceA < spotDistance) // if the player is very close
	{

		if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit))
		{
			if(hit.collider.gameObject.tag == "Player")
			{
				head.transform.LookAt(player.position);
				state = "pursuing";
				chasingPlayer = true;
				Player_is_Moving.chasing = true;
				isSeeking = false;
			}
			else
			{
				chasingPlayer = false;
			}
		}
	}

	else if(DistanceA < seekDistance && DistanceA < spotDistance)
	{
		isSeeking = true;
		head.transform.LookAt(player.position);
		chasingPlayer = false;
		Player_is_Moving.chasing = false;
	}
	if(chasingPlayer == true && isSeeking == false) //Chasing
	{
		state = "pursuing";

		this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
		                                           Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
		
		if(direction.magnitude > 5)
		{
			//this.transform.Translate(0,0,Time.deltaTime * speed);
			agent.SetDestination(player.position);
			agent.speed = 8f;
			anim.SetBool("isAttacking",false);
			anim.SetBool("isRunning",true);
			anim.SetBool("isWalking",false);
			anim.SetBool("isIdle",false);
			anim.SetBool("isSeeking",false);
		}
		else
		{
			agent.speed = 0f;
			anim.SetBool("isAttacking",true);
			anim.SetBool("isRunning",false);
			anim.SetBool("isWalking",false);
			anim.SetBool("isIdle",false);
			anim.SetBool("isSeeking",false);
		}
	}
	if(chasingPlayer == false && isSeeking == false) //Patroling
	{
		agent.speed = 2.25f;
		if(waypoints.Length > 0)
		{
			if(Vector3.Distance(waypoints[currentWP].transform.position, transform.position) < accuracyWP)
			{
				currentWP = (currentWP + 1) % waypoints.Length;
				if(currentWP >= waypoints.Length)
				{
					currentWP = 0;
				}
			}
		}

		anim.SetBool("isIdle",false);
		anim.SetBool("isWalking",true);
		anim.SetBool("isAttacking",false);
		anim.SetBool("isRunning",false);
		anim.SetBool("isSeeking",false);

		//rotate towards waypoint
		//direction = waypoints[currentWP].transform.position - transform.position;

		agent.destination = waypoints[currentWP].position;

		this.transform.rotation = Quaternion.Slerp(transform.rotation,
		                                           Quaternion.LookRotation(transform.forward), rotSpeedPatrol * Time.deltaTime);
	}
	if(isSeeking == true)
	{
		agent.speed = 1.65f;
		this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
		                                           Quaternion.LookRotation(transform.forward), rotSpeed * Time.deltaTime);
		agent.destination = player.position;
		anim.SetBool("isAttacking",false);
		anim.SetBool("isRunning",false);
		anim.SetBool("isWalking",false);
		anim.SetBool("isIdle",false);
		anim.SetBool("isSeeking",true);
	}
}

IEnumerator footsteps_Chase ()
{
	cantplay = true;
	audio.clip = footsteps[Random.Range(0, footsteps.Length)];
	audio.volume = 1.0f;
	audio.Play();
	HellHound.audio.pitch = S_Pitch;
	yield return new WaitForSeconds(canPlayTime_Chase);
	cantplay = false;
}

IEnumerator footsteps_Patrol ()
{
	if(isPatroling == true && Stand == false)
	{
		cantplay = true;
		audio.clip = footsteps[Random.Range(0, footsteps.Length)];
		audio.volume = .85f;
		audio.Play();
		HellHound.audio.pitch = C_Pitch;
		yield return new WaitForSeconds(canPlayTime_Patrol);
		cantplay = false;
	}
}

}

My enemy animator: Imgur: The magic of the Internet

For those’s still wanting an answer

Vector3 Target;
NavMeshAgent navMeshAgent; 
//create empty path
NavMeshPath navMeshPath = new NavMeshPath();
//create path and check if it can be done
// and check if navMeshAgent can reach its target
if (navMeshAgent.CalculatePath(Target, navMeshPath) && navMeshPath.status == NavMeshPathStatus.PathComplete)
{
      //move to target
      navMeshAgent.SetPath(navMeshPath);
}
else
{ 
       //Fail condition here
}

@Maxesone There’s a method called CalculatePath which allows you check whether a position is reachable before you move the agent. Here’s the documentation: