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?
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.