Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Spawning agents on navmesh area

Discussion in 'Navigation' started by Kouseii, Jun 19, 2023.

  1. Kouseii


    Jan 2, 2019

    I'm willing to make a spawner using the Object Pool for my agents. I have a 1000x1000 baked terrain with obstacles(trees and a small hills) and actually have no idea how to get a random point of the navmesh area and put the agent there.

    Can you guys help me to get the random baked area point?
  2. Inxentas


    Jan 15, 2020
    You're basically asking how to get a random point on an irregular surface. That would be super complex.

    Why not generate a grid of raycasts above your terrain, raycasting downward on the Terrain layer? Then check whether each point is on the NavMesh and walkable, and filter out those that are not. That would at least get you a limited set of contact points from which you could grab a random one. One downside might be that doing many many MANY raycasts (say... 1000x1000) may impact performance quite a bit. You may want to create a Collection of points once and then refer to this Collection when needed, and / or increase the distance between grid points.

    Alternatively, you could do a raycast using random X and Z values within the bounds of your terrain and repeat the process until a usable point is returned. Downside is that you don't know beforehand how many RayCasts will be done before a usable point is returned.
    androvisuals_unity likes this.