Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can I use NavMesh with TileMaps?

Discussion in 'Navigation' started by Abdo023, Feb 23, 2018.

  1. Abdo023

    Abdo023

    Joined:
    Dec 18, 2017
    Posts:
    64
    Is it possible to use NavMesh with the built-in Tile-Maps or Custom TileMap that I implement myself in a 2d Game ( y axis instead of z)?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Abdo023

    I'm not sure about this but...

    I don't think it is just an issue about plane you use to align your game world...

    I think navigation needs MeshRenderer component or terrain - and Tilemap is not using either, it is using TilemapRenderer.

    Maybe someone else can verify this, I've only been testingTilemap for a few days so far.
     
  3. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    I'm working on this exact problem. I have a tilemap that is updated frequently, so the Navmesh would have to be rebuilt or I'd need to put obstacles in the way. I could instantiate these, but I think both ideas seem like they would be slow, especially if I increase the tile count.

    The explosion demo script included in the demo assets - MouseClickBomb.cs seems like it could work for my needs. I'm thinking of having the AI check a list of forbidden destinations using the GridInformation.cs script attached to my tilemap. That way, the AI could check the list and re-target or re-path if their destination is a forbidden tile. In my case, it would be any grid cell with no tile in it.

    I'd be interested in hearing if you solved this a different way.
     
  4. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    The solution that worked for me was to create a cube with a NavMesh obstacle component on it for each tile. I used InvokeRepeating("UpdateObstacles") to update turn on/off the cubes. With this, I could update my tilemap every second, and with 10-50 agents get a decent FPS. It was too slow with 300 agents, and they just sort of froze. My Tilemap was 160 X 90 tiles.

    I think with a static tilemap, created at startup it would work much better. My agents had to constantly check that their destination tile was still valid, as the map changes a lot.

    Here are the methods that I used:

    Code (CSharp):
    1.     void SetupObstacles()
    2.     {
    3.         obstacleDictionary = new Dictionary<Vector3Int, GameObject>();
    4.         foreach (var position in map.cellBounds.allPositionsWithin)
    5.         {
    6.             Vector3 pos = new Vector3(position.x, 0, position.y);
    7.             GameObject g = Instantiate(m_obstaclePrefab, pos, Quaternion.identity);
    8.             obstacleDictionary.Add(position, g);
    9.         }
    10.     }
    11.  
    12.     public void UpdateObstacles()
    13.     {
    14.         if (obstacleDictionary == null)
    15.             return;
    16.  
    17.         foreach (var position in map.cellBounds.allPositionsWithin)
    18.         {
    19.             obstacleDictionary[position].SetActive(!map.HasTile(position));
    20.         }
    21.  
    22.     }
     
    eses likes this.
  5. StarGameStudio

    StarGameStudio

    Joined:
    Dec 6, 2016
    Posts:
    1