Search Unity

Navigation2D Official Thread - Use Unity's built in Pathfinding for 2D Games

Discussion in 'Assets and Asset Store' started by mischa2k, Jan 4, 2017.

  1. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    logo450px.png
    Navigation2D
    uses Unity's built-in Navigation system to make 2D pathfinding possible without any axis rotations. Navigation2D is on the Asset Store for almost two years now, so it's time for a forum thread.
    (Store) (WebGL Demo) (Documentation)

    Usage:
    1. Make 2D Colliders static
    2. Go to Navigation2D, select Bake
    3. Use NavMeshAgent2D to move

    Components:
    * Uses built in BoxCollider2D
    * Uses built in CircleCollider2D
    * Uses built in EdgeCollider2D
    * Uses built in PolygonCollider2D
    * Wraps NavMeshAgent for 2D
    * Wraps NavMeshObstacle for 2D

    Screenshots:
    newwindow.png
    navigation2d.gif
    mesh_wireframe.png
    groundscale.png

    Changelog:
    • V1.0 [2015-05-21]
      • Initial Release
    • V1.1
      • Smaller fixes and improvements
    • V1.2
      • Some refactoring
    • V1.3
      • 3D Projection position fixes
    • V1.4
      • 3D Projection rotation and scale fixes
    • V1.5
      • Upgraded to Unity 5.4.1p2
      • Fixed Internal_Create bug that happened in newer Unity versions
      • Fixed NavMeshObstacle2D Gizmo rotation
    • V1.6
      • Upgraded to Unity 5.4.2p3
      • NavMeshAgent2D hasPath added
      • NavMeshAgent2D isPathStale added
      • NavMeshAgent2D isOnNavMesh added
      • NavMeshAgent2D pathPending added
      • NavMeshAgent2D pathStatus added
      • NavMeshAgent2D remainingDistance added
    • V1.7
      • NavMeshAgent2D.Warp added
    • V1.8
      • NavMeshAgent2D now also works without a collider
    • V1.9
      • Upgraded to Unity 5.5.0p1
      • Converted all Boo Scripts to C#
      • Fixed a Circle Collider calculation bug where radius instead of radius * 2 was used for diameter
    • V1.10
      • PolygonCollider2D support
      • StackColliders workaround replaced with MakeUnwalkable method
      • Syntax Improvements
    • V1.11
      • IsValidCollider moved to Navigation2D script to avoid 'UnityEditor not found' issue when building
      • OnGUI Test button removed
    • V1.12
      • Fixed RotationTo3D using positive rotation
    • V1.13
      • NavMeshAgent2D more proxy functions added
    • V1.14
      • Upgraded to 5.5.1
      • NavMeshObstacle2D OnDrawGizmos uses Gizmo center
    • V1.15
      • Ground plane is now created from 2D Colliders
      • Triangulation polygon reversal enabled again
    • V1.16
      • Fixed an issue where some clockwise polygons weren't reversed
    • V1.17
      • Editor Window text changed to 'Make your 2D Colliders Static'
    • V1.18
      • Upgraded to Unity 5.6
    • V1.19 [2017-06-20]
      • Save and load settings via EditorPrefs
      • GUI shows Navigation2D logo
      • GUI instructions via HelpBox
      • GUI shows agentRadius + Diagram now
      • GUI bake and clear button alignment improved
      • Smaller syntax improvements
    • V1.20 [2017-06-27]
      • NavMeshAgent.path added
      • NavMeshAgent.autoBreaking set to true by default for best results
      • Agent.obstacleAvoidanceType added
      • agent.autoRepath added
    • V1.21
      • Fixed delayed Gizmo update by calling RepaintAll in OnBecame(In)Visible each time
      • Gizmo is now only drawn while the Navigation2D window is active to avoid possible performance impacts in bigger projects
      • Update is used for transform movement, FixedUpdate for rigidbody movement to make it smoother
      • NavMeshAgent2D component caching for performance
    • V1.22
      • Gizmos isn't always shown while Application.isPlaying anymore, now matches with built-in Navigation Gizmo behaviour
    • V1.23
      • Upgraded to Unity 2017.1.0p1
      • NavMeshAgent.truePosition added
    • V1.24 [released 2017-11-28]
      • Upgraded to Unity 2017.2.0p2
      • TilemapCollider2D support added. Works perfectly fine with Unity's new Tilemap feature now.
    • V1.25
      • Replaced deprecated .Resume() .Stop() with .isStopped
    • V1.26
      • Upgraded to Unity 2017.4 LTS
      • agent.velocity setter added
    • V1.27
      • EdgeCollider2D support added.
    • V1.28
      • NavMeshAgent2D properties are now truly proxies to the original agent. Serialized values that are shown in the Inspector are only applied in Awake once.
      • NavMeshAgent2D.LateUpdate copies position again just to be sure
      • NavMeshObstacle2D: obst renamed to obstacle for consistency
      • NavMeshAgent2D: default obstacle avoidance set to None because it doesn't work very well.
      • Upgraded to Unity 2017.4.17f1
      • Fix NavMeshObstacle2D.OnDrawGizmosSelected: use .rotation instead of .localRotation to fix gizmo rotation if parent object is rotated
      • Fix NavMeshAgent2D.OnDrawGizmosSelected: use .rotation instead of .localRotation to fix gizmo rotation if parent object is rotated
    • V1.29 [released 2019-06-21]
      • NavMesh2D.Raycast added
      • NavMesh2D.SamplePosition added
    • V1.30 [released 2021-01-17]
      • Upgraded to Unity 2018.4 LTS
      • fix: baked 3D Navmesh is now positioned at y=0 and 3D objects are positioned at y=0.5 so that their feet are positioned at y=0 on the NavMesh. fixes a bug where NavMesh2D.SamplePosition would require a minimum distance of 0.5 to make up for the distance between the projected y=0 and the baked navmesh y=-0.5 before.
     
    Last edited: Jan 17, 2021
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: I submitted V1.10 for review today. With lots of free time over the holidays I managed to make PolygonCollider2D work by generating 3D meshes from polygons via triangulation. This features has been requested a lot ever since releasing this asset, so I am sure a lot of you will benefit from it.

    I also converted it from Boo to C# a few days ago, which should make things easier for everyone.

    Navigation2D is pretty much done at this point, since it supports all the important features from the built in Navigation system. There are a few minor things like off mesh links and edge colliders, but I am not sure if many people even need those.

    Anyway, have fun with the asset and let me know if you have any questions.
     
  3. skerrigan

    skerrigan

    Joined:
    Jan 23, 2016
    Posts:
    3
    Hi,

    Do PolygonCollider2Ds work, as I'm trying to apply them to a map Am I doing something wrong? I keep getting some of the shore out of bounds, but not the green river I delienated.

    Here's how it looks - the river is the green slime...

    ha.png

    Stuart
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Interesting. Can you try a simpler polygon and see if that works?
     
  5. skerrigan

    skerrigan

    Joined:
    Jan 23, 2016
    Posts:
    3
    I had a similar result unfortunately.
     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Can you send me a simple scene where I can test it? It would be helpful if the polygon as simple as possible before the issue appears.
     
  7. XxPleYxX

    XxPleYxX

    Joined:
    Jul 26, 2011
    Posts:
    41
    I have the same problem with Polygon2D.

    - Unity 5.6.0f1
    - Import plugin
    - Create a Polygon2D, make static and modify it a little
    - Bake

    Result:

     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Do you use V1.15? I released a few bug fixes for polygons, this could be it.
     
  9. XxPleYxX

    XxPleYxX

    Joined:
    Jul 26, 2011
    Posts:
    41
    Yes, its the latest version.
     
  10. XxPleYxX

    XxPleYxX

    Joined:
    Jul 26, 2011
    Posts:
    41
    This is the only way i found, encapsulate the polygons in a boxcolliders2D box and bake. This is the result... a little bit tricky.


     
  11. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Interesting. Can you send me a minimalist project where I can test it myself?
     
  12. JaelsonCavalcante

    JaelsonCavalcante

    Joined:
    Nov 11, 2014
    Posts:
    4
    I just bought and have these errors, if you can help me.

    Unity 5.6 64 bits
     

    Attached Files:

  13. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Which Unity version do you use? Navigation2D currently uses 5.5, so that should work fine. 5.6 update will come in a few days.
     
  14. JaelsonCavalcante

    JaelsonCavalcante

    Joined:
    Nov 11, 2014
    Posts:
    4
    how long will it take for you to update the asset for unity 5.6?
     
  15. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    A couple of days, the asset store submission process usually takes a week though.
     
  16. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I just submitted V1.18 to the Asset Store. It will have the 5.6 update. If you want to use Navigation2D with Unity 5.6 and can't wait, then simply right click in the Project Area and select 'Reimport All'. Afterwards replace 'NavMeshBuilder' with 'UnityEditor.AI.NavMeshBuilder' in the Navigation2D script.
     
  17. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    I use isometric tiles for my walls, would I simple add a polygon collider to them so the nave mesh would make the path correctly?
     
  18. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes!
     
  19. UnityRocksAlt

    UnityRocksAlt

    Joined:
    Dec 28, 2015
    Posts:
    157
    Will you please fix these problems after import:

    Failed to create agent because it is not close enough to the NavMesh
    UnityEngine.GameObject:AddComponent()
    NavMeshAgent2D:Awake() (at Assets/Plugins/NavMeshAgent2D.cs:24)


    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:set_destination(Vector3)
    NavMeshAgent2D:set_destination(Vector2) (at Assets/Plugins/NavMeshAgent2D.cs:97)
    Move:Update() (at Assets/Navigation2D/Testscene/Move.cs:9)


    I cannot use your product, using Unity 5.5
     
  20. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Sounds like your NavMeshAgent2D is not on a Navmesh. Did you try baking it again? Does it happen in a fresh project?
     
  21. UnityRocksAlt

    UnityRocksAlt

    Joined:
    Dec 28, 2015
    Posts:
    157
    Yes it happens on a new fresh project. I haven't created anything except opening testscene. If you don't mind, the script Move.cs collides with other assets I have. Would you mind changing it to something else? Like pacMove, unusual so it does not collide.

    Would you mind mailing me previous versions, i have already sent you an email, I can send you the invoice number if you want. It's night here, to after 8 hours.
     
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Just tried it again, it defintely works fine with Unity 5.6.
    Tried it with 5.5.2 too, works as well. You have to close the project and delete the Library folder first though, then reimport it and rebake the NavMesh.
     
  23. UnityRocksAlt

    UnityRocksAlt

    Joined:
    Dec 28, 2015
    Posts:
    157
    Rebaking made it work on a new project.
     
    mischa2k likes this.
  24. Lautaro-Arino

    Lautaro-Arino

    Joined:
    Sep 18, 2013
    Posts:
    93
    Hi! Thanks for a simple and cool solution. Quick question: can you confirm that there is no way to give an agent width so it wont be able to go through passes that are too small?
     
  25. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Did you try the agent.radius property? You can modify it in the Inspector.
     
  26. Lautaro-Arino

    Lautaro-Arino

    Joined:
    Sep 18, 2013
    Posts:
    93
    Yes! It makes no difference. Its like its the smallest possible all the time.
     
  27. Lautaro-Arino

    Lautaro-Arino

    Joined:
    Sep 18, 2013
    Posts:
    93
    Here you can see that i have a big radius and the wolf can still go through any passage no matter how small.
    http://imgur.com/sdYE7aX
     
  28. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Interesting. Perhaps try the Window->Navigation(not 2D) settings with agent radius etc., that could work for now.
     
  29. Lautaro-Arino

    Lautaro-Arino

    Joined:
    Sep 18, 2013
    Posts:
    93
    So is it a bug or not? Should i expect the radious to affect where it can pass? Am i doing something wrong?
     
  30. Lautaro-Arino

    Lautaro-Arino

    Joined:
    Sep 18, 2013
    Posts:
    93
    Setting the radius on the Navigation(3D) does affect the baking! So there will be less narrow paths in the bake navmesh.
    But i have different radious on different agents. So the agents radius seems useless. Is this how its intended?
     
  31. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I just tried it in 3D with the built in Navigation system. My agent was able to move through a narrow path, no matter how big it is. As long as there is a path, it seems to be able to move through it no matter what.

    So since Navigation2D uses the built in system, that's 'normal'.
     
  32. Lautaro-Arino

    Lautaro-Arino

    Joined:
    Sep 18, 2013
    Posts:
    93
    Aha. Im pretty sure the 3D navigation is not supposed to to that though. But maybe ill search for an answer there. Thanks.
     
  33. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You are welcome. Let me know what you find out!
     
  34. MidknightNix

    MidknightNix

    Joined:
    Jan 10, 2017
    Posts:
    2
    Hey is it possible to get edge collider support for NavMesh 2D?
     
  35. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    It's possible yes. Can't it be replaced with one of the other collider types though?
     
  36. MidknightNix

    MidknightNix

    Joined:
    Jan 10, 2017
    Posts:
    2
    My coder tried to code the poly collider into edge collider but the results are inconsisten and he has no idea what to do?
     
  37. matthewlehew

    matthewlehew

    Joined:
    Jan 5, 2017
    Posts:
    1
    Hi! Is there planned support for the upcoming tilemap features found in the 2D Experimental Preview, including the Tilemap Collider 2D component? I understand these features are in development and there is no reason why Navigation2D should support them right now, just wondering if the developers are keeping an eye on this and planning to support the tilemap features when Unity releases them (planned release is 2017.2).
     
  38. Freddymx

    Freddymx

    Joined:
    Jul 27, 2016
    Posts:
    1
    Hi,
    I'm trying use Tiled2Unity generated polygon collider with Navigation2D but dosen't work.
     
  39. Astro-King

    Astro-King

    Joined:
    Jun 1, 2013
    Posts:
    2
    I bought the program in order to adapt it to use on a variety of enemies, but I've been having trouble adapting it from the point-and-click format. I thought that switching from
    Code (CSharp):
    1. void Update() {
    2.         if (Input.GetMouseButton(0)) {
    3.             Vector3 w = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             GetComponent<NavMeshAgent2D>().destination = w;
    5.         }
    6.     }
    to
    Code (CSharp):
    1. void Update () {
    2.         Vector3 w = (Player.position);
    3.         GetComponent<NavMeshAgent2D>().destination = w;
    4.     }
    (where Player is a transform defined on Awake) would be sufficient, but this has resulted in a strange jumping phenomenon for the enemies, especially when centered around tighter paths or Polygon colliders. Here's a video showing what I mean:


    I have little experience with C#, so I would appreciate it if you could give me an idea of what I should change to fix this. I've thought of rebuilding the Polygon Colliders as compound box ones, but I'd rather not do that unless it's necessary, and I cannot easily change the geometry of the scene to make wider paths.

    Thanks in advance for any help that is offered!
     
  40. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: submitted V1.19 for review. Will have a few GUI improvements:
    newwindow.png

    Sorry for the late reply, I missed the notification for some reason.
    What exactly are you trying to change? Settings the destination to the position will be pointless, since the player is already at its own position all the time. It looks like you should learn more about the Unity basics (no offense).
     
  41. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update: V1.19
    • Save and load settings via EditorPrefs
    • GUI shows Navigation2D logo
    • GUI instructions via HelpBox
    • GUI shows agentRadius + Diagram now
    • GUI bake and clear button alignment improved
    • Smaller syntax improvements
     
  42. Astro-King

    Astro-King

    Joined:
    Jun 1, 2013
    Posts:
    2
    Sorry if I didn't make it clear, but I'm actually using the script for enemies so that they can go around obstacles and reach the player, not for moving the player itself. Because of that, I needed to change their destination to dynamically update to the player's current location as he moves around freely. The unfortunate side effect that I encountered was that this caused the enemies to teleport to random locations, especially when going down slightly smaller paths.
     
  43. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Could be lots of things. You need to do some debugging.

    Try a much simpler scene with only one or two box colliders and see if it still happens.
    Try just two players and see if it still happens.
    If it still does, you can switch to 3D scene view while the game is running. Navigation2D uses the built in system, so you can see what the built in system does in 3D view. This should give some insight.
     
  44. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update:
    V1.20 [2017-06-27]
    • NavMeshAgent2D.path added
    • NavMeshAgent2D.autoBreaking set to true by default for best results
    • NavMeshAgent2D.obstacleAvoidanceType added
    • NavMeshAgent2D.autoRepath added
     
  45. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: V1.21 was submitted for review with a few more improvements:

    V1.21
    • Fixed delayed Gizmo update by calling RepaintAll in OnBecame(In)Visible each time
    • Gizmo is now only drawn while the Navigation2D window is active to avoid possible performance impacts in bigger projects
     
  46. cesarizu

    cesarizu

    Joined:
    Aug 30, 2015
    Posts:
    3
    Hello, is it possible to mix NavmeshAgent2D movement with another custom movement? Right now when I add the NavmeshAgent2D I can no longer move the agent manually. Thanks!
     
  47. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    What do you mean?
     
  48. cesarizu

    cesarizu

    Joined:
    Aug 30, 2015
    Posts:
    3
    I'm trying to control a character both with a joystick/keyboard (Input.GetAxis) and with the navmesh agent. My current solution is to disable the navmesh agent when there's input from the axis and re-enable if there's input from a click. I have to manually sync the position of the character and the generated "NAVIGATION2D_AGENT" too since it's not synced when re-enabling the NavMeshAgent2D component. For this I had to modify the NavmeshAgent2D file to make the agent member variable public.

    Is there a more clean way to do this?
     
  49. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Maybe use OnDisable to set enabled=false in the 3d navmeshagent too, and use OnEnable to set enabled=true and to warp to current position projected to 3d once.
     
  50. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi,
    Does Navigation2D work with Unity version 2017.1?
     
    kittikunJP likes this.