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);
}
}
}