- Home /
Navmesh problem with SpawnSystem
Hello everyone , I hope this has not been asked before (I could not find it)
I have created a simple spawning system for a tower defense kind of game to learn more about coding and the navmesh system. As long as I have only one agent walking along the navmesh everything works just find - however as soon as I spawn a second or third one - the first one will continue to follow its path, but all others will remain at their spawn point.
private void SetWaypoint(){ // store distance to the next waypoint distanceToNextWaypoint = Vector3.Distance(curWaypoint.position,myHeightPointer.position);
// if distance between waypoint and myself is smaller than minDistanceToWaypoint set my target to the next waypoint
if(distanceToNextWaypoint <= minDistanceToWaypoint){
// the path has a quadratic outline -> therefore do only increment the array number if I am not at the last waypoint already
if(int.Parse(curWaypoint.name) < (gameManagerScript.allWaypoints.Length-1)){
curWaypoint = gameManagerScript.allWaypoints[(int.Parse(curWaypoint.name)+1)].transform;
}
// start all over again
else{
curWaypoint = gameManagerScript.allWaypoints[0].transform;
}
}
// if I am too far away - I have to get closer to the current waypoint
else{
nav.destination = curWaypoint.position;
anim.SetFloat("Speed", 1f);
}
}
To further illustrate the case I have taken 2 screenshots . the first screenshot shows the totally confused AI-agents. Waypoints are all marked with their names ( 1-3 in red) + spawn waypoint (0 in green) , the actual navmesh in blue. The selected agent is the "working" first agent.
The second screenshot shows the components of the AI-agent.
[IMG]http://i40.tinypic.com/msfvrs.png[/IMG]
[IMG]http://i42.tinypic.com/x5v49j.png[/IMG]
It would be great if you could help me and tell me what I am -obviously - doing wrong. I know that I do not really need the navmesh system in this instance, but I would like to learn how to use it and furthermore if I somehow have to move the AI anyways. Thank you for your help. I hope I have given enough information.
BTW: I would have a second question. Is it possible to create a path with the navmesh so that (taking the example scene I have provided in screenshot 1) the agent will walk from waypoint 0 to waypoint 0 with the condition of passing through waypoints 1,2,3 ? The result is the same - but I would have the entire path. and could then have the cannonball tower aim a little ahead of the AI. Is that possible ? Or would anyone know a work-around for this problem ? Thank you for your help. :)
Best wishes,
Kergal
EDIT:
I have changed the allwaypoints from index 0 to index 1 in the start function - result . First agent still works, second agent works! until the first agent reaches the first waypoint . As soon as the first agent changes his waypoint (nav.destination) to waypoint 2 - the second agent will do the same thing. however the third agent will have waypoint 1 as its destination and will the just stop at waypoint one.