Cross posted on StackOverflow
I'm making a tower defense style game in 3D using Unity. I'm experiencing a frustrating issue with spawning large numbers of characters in an area of navmesh where if I spawn too many near the same location many of them will 'teleport' immediately after instantiation to near the navmesh destination.
Here's a doodle of one of the levels:
The black spot is the player base the NPCs have to get to. The green scenery has no navmesh and uses mesh colliders. Scenery is mostly steep hills forming a valley the NPCs should path through. It is composed of multiple meshes marked Unwalkable in navigation. The large blue spot is the first spawn location I tried, I later tried a larger area indicated in the right side of the image (see 'things I've tried' below).
When instantiating at the original spawn location if I spawn more than about 20 NPCs at once many of them will jump to the the area indicated by the red spots. The NPCs have a radius of about 1.5m, as do their colliders and navmeshagents avoidance property.
Things I've tried:
- Spawning to a random position inside a circle with a radius of 50m.
- Adding a large new hidden area of navmesh as in the image above, defining an array of vector positions spread well apart and spawning at each position in turn so no two navmeshagents spawn near each other.
- Adding a delay to
navmeshAgent.SetDestination(playerBase)
after instantiation hoping this would prevent the immediate jump to near the destination. - Reducing navmeshagents avoidance radius. This sometimes reduces the problem a little but still, even with a radius of 0.1 any more than around 50 NPCs triggers the problem.
- Turning avoidance off althogether (setting to none in navmeshagent), again, can spawn more but problem still happens
- Turning off auto traverse links and auto repath in navmeshagent
- Increasing the time between instantiations, a large gap of several seconds works but makes the game too easy
- Not setting a destination - oddly they still teleport to near the player base, so I guess destination is not a factor
How can I spawn large numbers of navmeshagents in a short timeframe, avoiding this 'teleporting' behaviour?