Search Unity

2D NavMesh PathFinding......

Discussion in '2D' started by Vinnie711, Nov 8, 2017.

  1. bikmoped

    bikmoped

    Joined:
    Sep 30, 2018
    Posts:
    3
    Hi vhman,
    The LayerMask support is working perfectly thanks man !
    However I think something is wrong in your last commit "added NavMeshBuilder2d - geometry form sprite mesh".
    I simply replaced the folder NavMeshComponent (the same way I did with the previous commits) and then when I tried to play on Unity the NavMeshSurface wasn't working anymore. The bake function looks broken. I tried a few tricks to make it work but I didn't manage.
     
  2. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Do you have errors or bake just doesn't do anything?
    Changes I made, ignores tilemap collider, now you should use navmeshmodifier override area option on tilemap.
    Also it now depends on sprites shape and not a grid. I'm working on grid implementation now.
    Give me some details, maybe I have some flaw in code.

    New update - Override By Grid flag. It determines what to use, sprites or provided grid mesh
    Code reference:
    Code (CSharp):
    1.   if (tile == Tile.ColliderType.Sprite && !builder.overrideByGrid) //use sprite as source
    2.   else if (builder.useMeshPrefab != null && builder.overrideByGrid) //use provided mesh
    3.   else //default to source shape Box
     
    Last edited: Feb 20, 2019
  3. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
  4. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    @Sharlatan Hello! I wanted to try your project, but immediately ran into an exception. Do you have any idea what causes this?

    Code (CSharp):
    1. ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    2.  
    3. Parameter name: index
    4. System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <3a07e6401b5d4cc98f7be596cb2130ba>:0)
    5. System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <3a07e6401b5d4cc98f7be596cb2130ba>:0)
    6. System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <3a07e6401b5d4cc98f7be596cb2130ba>:0)
    7. NavMeshSurface2DBaker.Triangulator.TriangulateConcaveOrConvexPolygon (System.Collections.Generic.List`1[T] vertices) (at Assets/NavMeshSurface2DBaker/Scripts/Triangulation/Triangulator.cs:111)
    8. NavMeshSurface2DBaker.MeshFromPolygonCreator.CreateBaseMesh (UnityEngine.Vector2[] polygonPoints) (at Assets/NavMeshSurface2DBaker/Scripts/MeshCreation/MeshFromPolygonCreator.cs:71)
    9. NavMeshSurface2DBaker.MeshFromPolygonCreator.CreateMesh (UnityEngine.Vector2[] polygonPoints, UnityEngine.Vector3 positionOfColliderPointsBelongTo, UnityEngine.Transform parentToAttachTemporaryObjectsTo) (at Assets/NavMeshSurface2DBaker/Scripts/MeshCreation/MeshFromPolygonCreator.cs:34)
    10. NavMeshSurface2DBaker.Surface2DBaker.ProcessCompositeColliders (UnityEngine.GameObject objectContainingObstacles, UnityEngine.Transform parentToAttachTemporaryObjectsTo) (at Assets/NavMeshSurface2DBaker/Scripts/NavMesh/Surface2DBaker.cs:125)
    11. NavMeshSurface2DBaker.Surface2DBaker.CreateMeshes () (at Assets/NavMeshSurface2DBaker/Scripts/NavMesh/Surface2DBaker.cs:74)
    12. NavMeshSurface2DBaker.Editor.Surface2DBakerEditor.Bake () (at Assets/NavMeshSurface2DBaker/Scripts/Editor/Surface2DBakerEditor.cs:37)
    13. NavMeshSurface2DBaker.Editor.Surface2DBakerEditor.OnInspectorGUI () (at Assets/NavMeshSurface2DBaker/Scripts/Editor/Surface2DBakerEditor.cs:26)
    14. UnityEditor.UIElements.InspectorElement+<CreateIMGUIInspectorFromEditor>c__AnonStorey1.<>m__0 () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorElement.cs:440)
    15.  
     
  5. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    @Sharlatan Here is the object's configuration:
    upload_2019-2-25_23-25-6.png upload_2019-2-25_23-25-47.png
     
    Last edited: Feb 25, 2019
  6. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Hi,

    it is not addressed to me, but rotation seems odd. It should -90 or 270 for default swizzle.

    Sorry, didn't saw exception problem. Skip it.
    The problem with polygon. You should look at https://www.habrador.com for details,
    maybe it doesn't work for holes or something. Try to debug in VS.
     
    Last edited: Feb 25, 2019
  7. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    @vhman unfortunately having the same index exception with both -90 and 270 degrees.
     
  8. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    @vhman I tried you solution also, but having another kind of error here: it seems to work nice with rectangular shapes aligned to axis (on the left side), but with collider surfaces rotated by 45 degrees it is inaccurate.
    upload_2019-2-26_0-47-34.png
     
  9. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
  10. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    upload_2019-2-26_0-52-22.png this is more detailed look. Have I configured component in a wrong way?
     
  11. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Hey, map looks interesting. Also it should work with diagonals, as now its uses Sprite Mesh. But I didn't tested on complex shapes. if you will share few sprites I can take a look if it capable of.
     
  12. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Assuming you have unity2d extras for tilemap installed, here is palette and the tile itself
     

    Attached Files:

  13. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Thank you for sharing. If you use Override By Grid it will use boxes for "outlining", but if I disable it, I see an issue that anything with rotation will be skipped.

    Thank you for sharing and feedback. I will fix it in few days.
     
  14. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    So right now it just always considers a tile to have strictly square collider?
     
  15. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Here is another issue that when Override by Grid is false it creates such weird gaps right in the middle of walkable areas.
    upload_2019-2-26_1-27-16.png
     
  16. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Yes, override by grid, means use grid instead of sprite. If override is not checked it will use sprite "shape". But as I said, it has an issue
    upload_2019-2-26_0-29-1.png
     
  17. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    I see now, thank you very much for a quick reply. Looking forward for this issue to be fixed.
     
  18. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Hey @TiredOfCoding,

    I have found issue. Translate first, and than rotate. Code is updated.
    Here is the reference code:
    Code (CSharp):
    1. src.transform = Matrix4x4.Translate(tilemap.GetCellCenterWorld(vec3int)) * tilemap.GetTransformMatrix(vec3int);
     
  19. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
  20. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Seriously, do you have sort of a roadmap for this project? I can see how a lot of people would benefit from it. And I myself would like to contribute to the project as much as I could, although right now I'm just a little familiar with the navigation system.
     
  21. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    I have no big plans, but there is few feature to implement:
    1. Generate mesh for Override By Grid from sprite - 90% complete
    2. Generate mesh for Override By Grid from Grid - 0% complete
    3. Ability to bake walkable modifiers with unwalkable default area - 0% (the first thing TODO on this weekend)
    4. Bake from physics (colliders) - 5% complete

    Now I need feedback to know should I spend my time on it. And you give me one, thanks.
     
  22. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Does the 3rd task mean that we would no longer need to attach area modifier to the tilemap? And does the 4th one mean that it would be possible to generate navmesh from other non-tilemap 2d colliders like a circle or a box?
     
  23. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    3rd - partially; 4th - yes

    PS NavMesh obstacles and volumes are supported
     
    Last edited: Feb 26, 2019
  24. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Could you clarify how the 3rd would be partially supported?
     
  25. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
  26. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Did you only change Area Type to Walkable, or there was some more work?
     
  27. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    @TiredOfCoding Hey there! Sorry, I missed your ping to me and only saw the message now.
    I see you're using vhman's solution now, which is great :)

    Nevertheless, if there's a bug in my project, I'd like to fix it of course! Unfortunately, I can't reproduce your error in my GitHub demo, though.
    If you'd still happen to have the problematic scene in question (or reproduce it) and provide it to me so I can investigate it, I would be very happy!

    Thank you very much and have a nice day!
     
  28. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    @Sharlatan Have you tried to build tilemap geometry using assets in cavern.zip I sent a few posts above?
     
  29. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    @TiredOfCoding Already got that and admittedly spent a good half hour or so trying to get that to work in one of my projects. That was why I was asking, because it would be way easier to get a small demo project that's already set up :D
     
  30. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Sure, I'll set up a small demo over the weekend. Thank you for the effort.
     
  31. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Thank you very much! But only if it's not too much trouble! :)
    Sorry, I couldn't get it to work! Or at least I don't think I did. All I could get out of the assets was this and it doesn't look like your cave at all..



    Currently I really can't say what's the problem. The code would probably not be able to handle non simple polygons (polygons with e.g. a hole in the middle) but if yo use a Unity tile map, you shouldn't have any non simple polygons, as far as I'm aware.

    Well, anyway. It would be highly appreciated if you find the time to put up a demo project and if not, it's ok too, of course! :)

    Have a nice day!
     
  32. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Do you think the fact that I use 2019.1 beta could cause an issue?
    Also, try it with non-rectangular tiles.
     
  33. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Tested some more and managed to get the error as well. Indeed, in some cases with very complex geometry, the composite 2d colliders seem to create some non simple polygons or mix up the order of the outline or.. I don't know yet.

    Honestly, with vhman developing such a good solution and mine being good enough for my simple cases, I probably won't investigate how to fix this, since it might take anything from a few hours to a few days. Unless, someone really, really needs it but I'd suggest to use vhman's solution anyway ;)

    So, you of course don't have to provide me an example anymore but your help is really highly appreciated, thanks very much once again!
     
  34. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    53
    Alright then, I will stick with vhman's solution since it works perfectly for me. Good luck with Devils of the Sea!
     
  35. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    11

    I have a Tilemap2DCollider and 2 Tilemaps, first the Ground with Walls and Second some Objects.
    It ignores the Objects but i have on both types the NavMeshModify Component and Override Area activated.

    But why?
     
  36. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Last edited: Mar 5, 2019
  37. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Oh, I didn't saw the "Objects" is also tilemap tiles. The problem can be in Z axis. All maps should be flash to mavmesh, like (x: *, y: *, z: 0). If its not a problem, share your navmesh and modofiers settings. You cam use personal massages if you want
     
  38. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    11

    All Transforms are "0, 0, 0" i use two Tilemaps (one for the Ground and Walls, one for other Objects), but it only uses the first Tilemap..
     
  39. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    11
    It worked when i change the Tile Anchor Z to the same as the other, you can make some changes -> On Build all Z Anchors are ignored.

    Next i changed is, the Agent Radius to 0.1, High to 0.3 and step High to 1

    Sry for bad english i'am from Germany.
     
  40. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    11
    So next Problem: My Agent won't move if i set agent.destination.
     
  41. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Hi,

    Z anchor still elevates tiles, so their position is not flash with NavMesh as required. It would be difficult to implement and handle it with swizzles and anchor. So yes, best is to just override Z in code(or other axis).

    I'm not experienced with Isometric tilemap, I will check it out on weekend, and add to examples.

    But I have a question, why are you using Z axis? I thought you need just to sorting order and not 3rd axis, Unity has new feature to sort same ordering by axis. Check https://docs.unity3d.com/ScriptReference/TransparencySortMode.html
     
  42. uweenukr

    uweenukr

    Joined:
    Jan 17, 2011
    Posts:
    53
    @vhman first off so far I love this!

    Is it expected that when you scroll out some and bake that it just keeps getting bigger? In the below screenshot i exaggerated a little. Is the expectation to use the 'Collect Objects' and manually set the size of the map?

     
  43. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Hi,

    Known issue. Now it using Tilemap as world bounds, and when palette or painting instrument selected, Unity extends Tilemap when you move cursor. So select any other editor tool, like "hand", before baking. It a bit annoying.

    This part should be redesigned, as some worlds are not rectangular shapes.
     
    Last edited: Mar 12, 2019
  44. uweenukr

    uweenukr

    Joined:
    Jan 17, 2011
    Posts:
    53
    Workaround worked great. Ty!
     
  45. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    11
    @vhman you have make a update?

    Its buggy with many differend tilemaps.
     
  46. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
  47. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    90
    This is some really interesting work and I'm toying with it right now and noticed something really annoying that isn't your issue per se but wondering if you can resolve it.

    Apparently if you draw a tile on accident anywhere in the world and erase it it expands the tilemap bounds to that location thus creating an insanely huge blank baked navmesh.

    Is there any way to actually confine the nav to placed tiles opposed to whatever internally is stored? That's actually really horrible to find out that the tilemap is 10x larger than intended because I accidentally drew on the wrong tilemap and erased it. Unity should fix this.
     
  48. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    Thank you @TSabos for feedback.

    As far as I know there is an issue described here that is very annoying. All other is design flaws without right solution.
    There is command CompressBounds (can be called form Edtior) that can be called before baking, but it is time consuming.
    There is a lot of possible solutions: adding flag to run resize once, run resize every bake, creating EditorScript to resize tilemap by hotkey.

    But obviously in dev (before production) it doesn't matter how you tilemap extends beyond its visible sizes, except visual discomfort.
    Good game design will be to: (1) create invisible map border any object cannot leave; and (2) bake only visible tiles.

    If you experimenting with the shapes you can use next settings: (1) set default area as unwalkable; (2) add navmeshmodifier with area override as walkable, - as result only visible tiles will be in navmesh.

    In any case I'm adding your feedback in todo list ;-)
     
    Last edited: Mar 26, 2019
  49. juanitogan

    juanitogan

    Joined:
    Aug 18, 2017
    Posts:
    25
    @vhman - So, to be clear, your solution isn't ready for 2D colliders yet? I don't have a tiled 2D world -- it's all 2D physics. Thus, I guess I need the @Sharlatan solution.
     
  50. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    80
    @juanitogan, yes, it will not generate navmesh form collider polygons. It is for tilemaps and sprite's meshes.