NavMeshAgent.CalculatePath returns true on first frame, should return false

Hello,

I have a little problem regarding the NavMesh-Calculations.
In my game theres an agent, who is trapped between dynamic carving obstacles, he should not be able to reach the target.
In the Start()-function, NavMeshAgent.CalculatePath == NavMeshPathStatus.PathComplete, which should not be the case.
I added a little time-buffer to recalculate the path after a certain amount of ms, it gives the right result than, but I already can feel thats bad practice.

My guess is that the obstacle carving takes longer to calculate than the first frame.
Is there anything I can do about that, like, is there a “WaitForCarvingToFinish” or something?

public class NavMesh : MonoBehaviour
{
    [SerializeField] Transform target;
    [SerializeField] NavMeshObstacle obstacle;
    public NavMeshAgent navMeshAgent;
 
    NavMeshPath path;

    bool pathCheckedTwice;
    float timer;


    private void Start() {
        path = new NavMeshPath();      
        pathCheckedTwice = false;
        timer = 0;
        RecalculatePath();
    }

    private void Update() {
   

        //VERY BAD CODE! Waiting for dynamic obstacles to finish calculating before checking Path
        timer += Time.deltaTime;
        if(timer >= 0.05 && !pathCheckedTwice){
            RecalculatePath();
            pathCheckedTwice = true;
        }

    }

    //gets called when mousebutton is released after dragging dynamic obstacle
    public void RecalculatePath(){
    
        if (navMeshAgent.CalculatePath(target.position, path))
        {       
            if (path.status == NavMeshPathStatus.PathComplete){
                navMeshAgent.destination = target.position;
                navMeshAgent.isStopped = false;
            }
            else{
                navMeshAgent.isStopped = true;
            }
        }
    }
}

You can try using this event:

1 Like

that looks very promising. I‘ll try it. Thanks a lot!