- Home /
Unity 2d navmesh for a tilemap
Hello, I have made a 2d world in unity using tilemap. My grid has two tilemaps, one for the walkable space (ground), and another for the walls (trees), which has a tilemap collider. I can place a controllable character in my map and collide with walls as I walk around the board. I am now trying to create a navigation mesh so I can give my character the ability to click to move around the board. I have heard that the unity navigation mesh does not work with unity 2d, is this still the case? If so, I have already rotated my grid and tilemaps to be aligned with the xz plane. Now that my playing field is aligned with the xz plane, is there a way to generate a navigation mesh onto my grid and these tilemaps? I am aware that I can make an invisible 3d world that aligns with my tilemaps for the floor and walls, but I am wondering if there is a simpler solution.
Answer by beanie4now · Aug 19, 2018 at 07:19 PM
EDIT @Compguru910 I have been working on this for the past month and have everything running smoothly. Here's how to do it.
Start new project as 2D
Import the following components https://github.com/Unity-Technologies/NavMeshComponents https://github.com/Unity-Technologies/2d-extras
Add a new 3D plane to the scene and rotate it -90 on X axis
NEVER i repeat NEVER open the "navigation" tab at any time
Add a "navmesh surface" component to the Plane. Bake it... it should turn blue.
Create a desired EMPTY GAMEOBJECT prefab with a child that you add navmesh agent and another that you add sprite and animation to (i recommend an empty parent then a separate nav and sprite child. The sprite will want to rotate to lock onto the navmesh so just write an update transform that makes the sprite follow the nav agent)
Create a desired obstacle prefab as well then add the component "NavMeshObstacle" MAKE SURE the obstacle has the box "carve" checked
Make the obstacle a prefab by dragging it into the project window below.
Right click the project window, select "Create", "Brushes", "Prefab Brush"
Set length to 1 then drag in your prefab obstacle
Paint a few onto a tilemap and then when you click on the plane you should see holes in the navmesh surface.
Add some code to move around like such
void Update () { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (!EventSystem.current.IsPointerOverGameObject() && canMove == true){ nav.SetDestination(hit.point); } } } }
And of course below all the tilemaps I have a 3D plane that I can just set as a navigation static and bake once all of the prefab tiles have been placed.
How exactly are you using that plugin? I downloaded and imported to my project, but I'm not seeing editor windows added to use the new functions. Any insight would be greatly appreciated.
I edited my Answer with a step by step guide as to how I have $$anonymous$$e running now. Hope it helps!
It works!! @beanie, how do you cleanup those obstacle prefabs?
I haven't used Unity in a while but I think there are 2 ways. The erase button in the tile map window may work... use like a brush. If not the prefabs will be in the hierarchy as children of the tilemap you placed them in. You can select them from there if all else fails.
I disabled the mesh renderer and it did the trick! thanks!