6
\$\begingroup\$

I'm working on game that needs navigation and obstacle avoidance. I've used nav-mesh on 3d project before but now I'm trying to use it in 2d sprite game but it seems like it doesn't work.

I want to know if really it doesn't work and, if it doesn't, what would be a good replacement for a 2d project for the navigation of entities.

I'm looking for free tools.

\$\endgroup\$
5
\$\begingroup\$

You cannot use the Unity Navmesh in 2D, you would have to write your own. As far as I know, there are no free nav mesh tools available.

Alternatively, you could use a Nodal Pathfinding approach, as detailed here:

http://www.jgallant.com/nodal-pathfinding-in-unity-2d-with-a-in-non-grid-based-games/

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ yes. searched in asset store. there is no free one. im going implement my own A*. im now looking to your demo. thank you for helping \$\endgroup\$ – virtouso Mar 2 '16 at 23:22
10
\$\begingroup\$

In case anyone Googles this later, you CAN use nav meshes in Unity. I'm using unity 2018.2 and set up a navmesh in a purely 2D top down tilemap game I'm working on.

Check this thread in the Unity forum: https://forum.unity.com/threads/2d-navmesh-pathfinding.503596/

Relevant posts start at #14. Fortunately, the poster created a repo for the code on GitHub: https://github.com/h8man/NavMeshPlus

It comes with a PDF Howto explaining how to use the script to bake your NavMesh.

The important part to remember is that the NavMesh works on the XZ based grid, which means that once you've set up the NavMesh and added the NavMeshAgent to the game object acting as an agent, NavMeshAgent will rotate the agent x=90, putting them perpendicular to your 2D XY grid.

The easiest way I found to deal with that is to add an internal object to the agent and set its rotation to x=-90 to negate the NavMeshAgent rotation.

Do NOT try setting the Grid swizzle to anything other than XYZ or the Nav Mesh Surface 2d will not bake correctly.

Once you've adjusted the agent's rotation, using NavMeshAgent's SetDestination works as it should. My agents are happily chasing my player object around in 2D with no issues.

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

As already answered the navmesh does not work in 2D, but you can fake it by creating a 3D scene with just colliders, set an orthographic camera from above and attaching sprites to the 3D objects.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Bake the Navmesh onto your scene (xz) and emit all things to 2D (xy).
ref: Navmesh2D concept

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.