- Home /
Instantiate AI characters over Navmesh area?
Hi there, as the above question states, is it possible using Unity library methods or otherwise to instantiate, in my case randomly generated AI characters, but they only spawn over the area the Navmesh covers?
I.E. The AI are a crowd of people in a city, but I don't want them to spawn in for example the middle of the road, which I have specified as non-walkable to the agents/characters in the road's navigation layer.
Similarly what would I do to randomly change the setDestination of the AI agent to keep within the bounds the navmesh covers? I hope I've helped you grasp the situation! :D
This is probably quite simple, but then again, I could be very wrong.
Does nobody have an answer? I hate to do this but... BU$$anonymous$$P.
Answer by Toli · Aug 12, 2013 at 01:58 PM
I've been working on some similar ideas and looking at AI code, so i can't quite help with code examples, but here's how i'd start it off. remember to start small, but plan big.
With multiple AI's, use a pool or spawn manager.
which would manage the variables for: starting point(s), a route or unique characteristic /waypoints to use for each spawn. a maximum number of mobs to spawn.
and, possibly some other characteristics you'd want to alter, like
textures for spawning a variety of mob types or some random variables for color/shape/size
max idle time, max walking time, speed, accel, turning speed, health, audio effects to use for idle/walking
etc.
a spawn manager can have this all 'baked in' with variables, or you could find a spawn manager, edit it to include these variables.
some AI scripts also come with a few examples of a pool manager i.e. tower defense games have some good examples of spawn and pool managers to handle timers, movement, re-navigation, etc. the pool manager would handle timers, navigation updates in bulk and save some performance issues with so many AI's, pool managers also make a good place to put in any collision tests and so on.
it may require some significant testing ... sic.
if all the mobs share one general path, you can also optimise the calculations and cache waypoints or create "fuzzy" waypoints, between A and B or randomly move the end point to a position near to B, so they don't all stand on one spot to turn.
the pool would set mobs to come out from say, 2 - 4 different positions, the mobs will each pick a navigation node, then walk from a list of defined spawns or walking points in the city, maybe A to G, and loop either on an idle, or a walk cycle. (or just have them walk on the navmesh around the city full time.)
alternatively, you could spawn and despawn characters off-camera as well, by checking that the spawn point is safe.
first, spawn an initial, hidden object on the navmesh, run some checks to see if it's colliding, near to any other AI's, or in range/raycast range of any other objects like the camera, and if it's all clear, spawn the AI prefab in the selected location, or if a character is stuck, can't navigate, or they have been in the same position for too long, have it idle, then respawn in a new off-camera location.
etc.
for more natural behavior, a few random numbers or timers can help. each update it can choose a new route to take once it gets to the destination, or have them pick a new waypoint after 20-40 seconds. with an idle/walk timer, it will walk for a while, stand, choose a new waypoint after it has idled, walk towards it, repeat.
collision, could cause a reset of the timers and the destination of the AI, so they could walk to the same destination, one collidee' would idle, the other collidee would choose a new destination, etc.
this way, each AI will properly mill around, and change direction every so often. this would keep the AI's from becoming too similar or chained together, you probably should keep the idle timer low, and the walk timers longer, say 4s idle /20 seconds walk, or base it off the duration of the idle animation, etc.
a pool manager can also handle enemy and player interaction/radius, or scatter/scare if you want a more GTA iV like NPC to start running when they take damage, see the player is in range, trigger the scream/terror audio, etc.
you get the idea.