Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Checking existance of Navmesh

Discussion in 'Scripting' started by Terminus001, Apr 22, 2019.

  1. Terminus001

    Terminus001

    Joined:
    Nov 1, 2015
    Posts:
    121
    Hi everyone,

    I am using the following code to generate a random position for my AI to reach while patrolling an area. Before choosing the next location to reach, the AI performs a check to see if the Navmesh exists on the location of the next target.

    Code (CSharp):
    1. NavMeshPath sentiero = new NavMeshPath();
    2.             bool esisteNavMesh = agenteScript.CalculatePath( new Vector3(xVal, yVal, zVal), sentiero);
    3.             while (esisteNavMesh == false)
    4.             {
    5.                 xVal = Random.Range(posIA.x - 5f, posIA.x + 5f);
    6.                 zVal = Random.Range(posIA.z - 5f, posIA.z + 5f);
    7.                 yVal = Terrain.activeTerrain.SampleHeight(new Vector3(xVal, 0, zVal));
    8.                 esisteNavMesh = agenteScript.CalculatePath(new Vector3(xVal, yVal, zVal), sentiero);
    9.             }
    Unfortunately this is not robust. 10% of the times it will pick a spot which lays outside the Navmesh. This means that the method CalculatePath is not working as I imagined.

    Anyone knows why?

    Thanks in advance.
     
  2. Deleted User

    Deleted User

    Guest

    Maybe you should install the navmesh on a plane that covers the ground instead of on the ground itself? ;)
     
  3. Terminus001

    Terminus001

    Joined:
    Nov 1, 2015
    Posts:
    121
    Is that really necessary? If I have huge maps this seems quite a tedious process.
     
  4. Deleted User

    Deleted User

    Guest

    Well, you can do that in small places at least. ;)
     
  5. Terminus001

    Terminus001

    Joined:
    Nov 1, 2015
    Posts:
    121
    Thanks. But I have the feeling that doing this won't necessairily solve my problem, because the exact same process will be performed on the plane then.