Question by
agarwaldhruv792 · Apr 20 at 10:14 AM ·
spawnnavmeshnavmeshagentspawningnavagent
How to spawn a navmeshagent on a certain nav mesh?
I have a scene with a navmeshAgent and houses in it.. I want to spawn my navmeshAgent randomly in any house... Is there a way I can do this?
Comment
Answer by HalilAlper · Apr 20 at 10:56 AM
To spawn agents on navmesh randomly, firstly you can use NavMesh.CalculateTrangulation()
and then you can pick randomly from these triangles.
NavMeshTriangulation Triangles = NavMesh.CalculateTriangulation();
int RandomNumber = Random.Range(0, Triangles.vertices.Length);
To make them only spawn in the houses, you can put box colliders inside the houses and use a Physics.OverlapSphere
on that location to make sure that location is inside the house, so therefore, inside the house collider. If not, cancel spawning.
For better reference, here's a video over NavMesh Triangulation and spawning enemies randomly, but it doesn't talk about spawning at specific zones as far as I know.