NavMeshPathStatus seams to be a little bit hasty

Hello,
iam working on a puzzlegame where you have to rotate different maze peaces so that the player and the exit point are connected. so far so good.

iam currently working on the auto acknowledge to check if the way from the player to the exit is possible or not, if yes, let the player jump around.
if (path.status == NavMeshPathStatus.PathComplete) {

my problem now is, that the NavMeshPathStatus is reporting to early which looks like this in game:
alt text

so you see, the indicators is turning green even if the path is not complete and then it switches back to red because he realized that he made a mistake. pretty cute that he is so ambitious but useless :wink:

here is the complete code:

using UnityEngine;
using System.Collections;
using plyBloxKit;


public class NavMeshCheck : MonoBehaviour {
	public Transform target;
	public GameObject Ampel_Green;
	public GameObject Ampel_Red;
	public GameObject Attention;
	private NavMeshAgent agent;
	public bool Path;

	void Awake() {

		Ampel_Red.SetActive(true);
		Ampel_Green.SetActive(false);

		Attention.SetActive(false);

	}

	void Update() {
		agent = GetComponent<NavMeshAgent>();
		NavMeshPath path = new NavMeshPath();
		agent.CalculatePath(target.position, path);
		//GetComponent<Animation>().Play("Ball_Jump");


		if (path.status == NavMeshPathStatus.PathComplete) {

		
				Path =true;
				Ampel_Green.SetActive(true);
				Ampel_Red.SetActive(false);
				
				Attention.SetActive(true);
				//plyBloxGlobal.Instance.SetVarValue("Puzzle", false);
		

				Debug.Log("PathComplete");


		}
		else {
				
				Path =false;
				Ampel_Red.SetActive(true);
				Ampel_Green.SetActive(false);
				
				//plyBloxGlobal.Instance.SetVarValue("Puzzle", true);
				Attention.SetActive(false);
				
				Debug.Log("PathInvalid");


		}
	}
}

the scene setup is pretty easy, on the floor is a navmesh, and if the level gets loaded some maze parts will get instantiated which inculding some navmesh obstacles. idea comes from here: http://forum.unity3d.com/threads/check-if-two-rooms-are-connected.331068/

Hey, I remember that forum thread!

So, whenever you move the pieces, your navmesh obstacles stops carving (making holes in the navmesh) until they’ve stopped. Since you’re checking every frame in Update if the path exists, this problem will happen.

The best solution is to not use Update here, but instead only check after a piece has moved. So change your void Update into public void OnPieceRotated, and call it whenever a piece you have rotated has stopped moving.

As a bonus, you’ll also gain some performance, as checking the path every frame means checking the same path hundreds of times in a row.