NavMeshAgent does not wait for path to be completed before moving to next destination

Hello everyone!

I have attached my little script (see below) to a tree (gameobject). The idea is that once one of my bots has reached a tree, it will chop it down, thus a broken tree gets spawned. That works. Then the bot gets away from the falling tree not to get hit. That also Works.

Now once the bot has completed his path to the secure distance, I want him to get back to a child component of the destroyed fallen tree model and continue chopping it (in this case the log). But for some reason that has already cost me like 6 hours (and various unsuccessful attempts with different methods - starting with the most simple one: if (agent.transform.position==newPosition) {move to next position}, the bot either interrupts his path to the secure distance and gets right back to the log or moves to safe distance but never comes back (like in the method i pasted below).

Any ideas what the problem is? Would be sooo thankful for any hint. Here is my script:

IEnumerator OnTriggerEnter(Collider other)
    {


        //Debug.Log("My Object Name is " + gameObject.name + "and i collided with something");
        if (other.gameObject.tag == "Bot")
        {

            // get the bot thats collided with the tree
            NavMeshAgent agent = other.gameObject.GetComponent<NavMeshAgent>();
            Debug.Log("NEW DEBUG: I am the Object " + gameObject.name);
            Debug.Log("NEW DEBUG: Object that collided with me: " + other.gameObject.name);

            //chopping (just a wait for now)
            yield return new WaitForSeconds(1);
            Debug.Log("Chopping the tree for 1 seconds ");

            //move the agent away from current destination not to get hit by tree
            agentPosition = agent.transform.position;
            newPosition = new Vector3(agentPosition.x - 8f, agentPosition.y, agentPosition.z);
            agent.destination = newPosition;
            yield return new WaitForSeconds(0.4f);

            //destroy tree

            Destroy(gameObject);
            Debug.Log("The tree is destroyed to make room for the new tree");

            // spawn the destroyed tree model and assign save the spawned object in a variable

            treeLog = Instantiate(destroyedTree, gameObject.transform.position,gameObject.transform.rotation);
            Debug.Log("I have spawned a broken version of the tree at " +treeLog.transform.position + "with the object name" + treeLog.name);


            //Get Position of Child of spawned Gameobject (tree) that i want the bot to move to
            logOnly = treeLog.transform.GetChild(1).gameObject;

            Debug.Log("I have identified the child of broken tree (the log) with the ID 1  " + logOnly.transform.name + " with coordinates " + logOnly.transform.position);
            newPosition2 = logOnly.transform.position;

            //Make sure the bot has reached its destination before moving to new destination
            if (!agent.pathPending && !agent.hasPath)
            {
                Debug.Log("I have reached my destination!");
            
                agent.destination = newPosition2;
                Debug.Log("SUCCESS: Moved to" + newPosition + " originally " + logOnly.name + " at " + logOnly.transform.position + "after a little delay" + "position check (should be equal)" + agent.transform.position);

            }

            Debug.Log("SUMMARY: I am the Object " + agent.name + " at " + agent.transform.position + " in the script attached to" + gameObject.name + " and i spawned the broken tree with the name" + destroyedTree.transform.name + "actually called" + treeLog.name + "and i have successfully moved to its child " + logOnly.name + " at position " + logOnly.transform.position);


        }

    }
}

It’s only a guess but you have to wait until newPosition is reached before setting newPosition 2.
Now if the condition

 if (!agent.pathPending && !agent.hasPath)

is not met you never set newPosition 2.

The code has to look sth like that:

//wait until agent has reached newPosition
while(condition)
{
   yield return null;
}

agent.destination = newPosition2;

Also regarding the conditions:
I had sometimes problems with agent.pathpending so instead i am checking the distance to the position and magnitue of the agent:

//wait until agent reached current destination
do
{
var distance = (newPosition- agent.transform.position).magnitude;
yield return null;
}
while(distance > agent.stoppingDistance && agent.velocity.magnitude >= 0.1f);

//Set new destination
agent.destination = newPosition2;

Claudia!
Thank you so much for taking the time to read my script and propose a solution. :slight_smile: :slight_smile: :slight_smile:
I now use your script for moving, however i figured out that that was not the problem. But it gave me the chance to find the actual problem. So thanks a lot in both ways!!

The problem was that I destroyed the gameobject to spawn a new one. I guess since this is a collider, once i destroy the original gameobject that collider is assigned to, I can continue the script and do almost everything, but any navmesh interaction that i do with i.e. coordinates of newly spawned object wont work anymore.

Not sure if this is a bug, but its at least very misleading, especially since the rest of the script works fine after destroying the object.