Search Unity

2D NavMesh PathFinding......

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

Thread Status:
Not open for further replies.
  1. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    @MelvMay Could you please provide a small tutorial on how to set up the environment?

    In my case I have a grid with collider, which I put the NavMeshSourceTag2D component on, and then press 'bake' on NavMeshBuilder2D, which is on the parental object relative to the grid collider. But somehow I cannot see the nav mesh appear on the scene.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Unfortunatley I cannot provide a tutorial and that code hasn't been released officially and isn't supported at all. I'm not actively working on this either, other devs are. I implemented the mesh-creation stuff for 2D colliders only.

    The way hackweek works is that a project is worked on by devs who often don't normally work in that area. In this case, this feature is being worked on by devs who have to allocate time to it specifically post hackweek. Unfortunately that means a delay. AFAIK Sept will be when it's worked on. I was hoping it'd be soon after hackweek but unfortunately that wasn't the case even though I got the native change in very quickly.

    I'll contact the devs and ask if they have any ideas though. I do recall there being some issues about the bounding box and it being rotated but nothing that required a native fix, just something to watch out for. I'll see if I can find out for you.
     
  3. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    Thank you very much, looking forwards to use it when it's ready!
     
  4. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Just started new branch for 2019.3 and new feature "Physics Colliders", to use Mesh from colliders (see above).
    And it kind of works. After I will resolve most of the issue, source code will be available in my repository.
    upload_2019-8-21_2-18-54.png
     
    allenk00 and MelvMay like this.
  5. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi All.

    Now NavMeshPlus can bake sprites placed inside Grid. https://github.com/h8man/NavMeshPlus

    And here it is: https://github.com/h8man/NavMeshPlus/tree/PhysicsColliders.Unity2019.3
    New branch for 2019.3 and new feature "Physics Colliders"
    It does not work for all composite colliders in test scenes for RedHotSweetPaper, but you can experiment.
    Sometimes collider.CreateMesh(false, false) return null, so watch for the console logs.
    Here is comparison of master and PhysicsColliders.Unity2019.3 branches
    upload_2019-8-22_1-4-6.png
     
    allenk00, srepollock and MelvMay like this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    It returns null if there are no shapes to create. The only other case is where if it sees edges where the last vertex overlaps the first vertex which on a composite is always the case (when outline mode is on) so that's very odd.

    You can see this on an EdgeCollider2D with EdgeRadius=0. Create a few lines and you'll see no mesh created from it (infinitely thin line). Then change the last edge so that it overlaps the first creating (say) a box. It'll create a mesh from that.

    If you have a reproduction case where it returns NULL indicating a mesh cannot be created where you think it should then it'd be great to see that.

    EDIT: Thank you for adopting this, it's very much appreciated. Ping me if you need anything.
     
    Last edited: Aug 22, 2019
    allenk00 likes this.
  7. Node

    Node

    Joined:
    Jan 31, 2013
    Posts:
    1
    hi folks. first of all, thanks vhman for the package! i'm building something similar to a 2d strategy game but i'm having a few problems: there are around 5 agents or so controlled at once that should have the least influence on each other as possible, so the radius and height should be as tiny as possible. i tried it with very small values (also baked the navmesh with these values), but if it's smaller than 0.1 the agents sometimes don't move at all. i also have a navmeshlink with a broader width that closes the gap between two walkable areas, but the agents sometimes don't take the link at all (no matter their radius) or just after a few seconds of not moving. also a weird thing: the navmeshlink doesn't show up in the editor if i pick the correct values (x and z start and end 0, only y start and end differ, if i click align transform, it sets z values to -0.05), but it still works (as above, only sometimes).
    any explanations of this? did i configure something wrongly? i tried it with several agent sizes but they keep stopping occasionally and also seem to have problems steering around obstacles and other agents
     
  8. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
  9. SomeRandomGuy

    SomeRandomGuy

    Joined:
    Jul 4, 2012
    Posts:
    11
    @MelvMay In reply to #152, has there been any work done to make 2D Navmesh work yet since september is coming to a close? I'm making a topdown 2D game with a friend, and was super excited when I remembered the navmesh system existed as it would make the whole thing that much easier to code. Seeing workarounds exist is great, but if it's going to be supported out of the box soon I may hold off on pathfinding for now and put my efforts elsewhere for a while. Thanks in advance! :)
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    I'm personally not working on it but last week I did ask the dev (who was scheduled to work on it end of Sept) said they should be able to after Unite (which was last week). As always, don't hold-up your development because a feature might be coming in the future.
     
    Green11001 and SomeRandomGuy like this.
  11. SomeRandomGuy

    SomeRandomGuy

    Joined:
    Jul 4, 2012
    Posts:
    11
    Aye, nice to know it may be coming soon then! We're making the game mostly as a little hobby project so hold-ups aren't too bad. Probably going to depend on how soon we finish other stuff, but if we do get stuck because of it we'll definitely follow your advise and just go with one of the workarounds out there, that's not a problem. Thanks for the quick response! :D
     
    Mohave_j likes this.
  12. Ocean257

    Ocean257

    Joined:
    Jan 9, 2016
    Posts:
    16
    I want to bake at runtime as my map is procedurally .
    Is there any way to do it?

    EDIT ::
    NaveMeshSurface2d has function called BuildNavmesh() Which did my job
     
    Last edited: Oct 22, 2019
    vhman likes this.
  13. Ocean257

    Ocean257

    Joined:
    Jan 9, 2016
    Posts:
    16
    Navmesh Modifier only works when it is added to a component which has Renderer component in it
    It does not work for the component which has colliders.
    Is there any way where it works when the navmesh modifier component is added to colliders?
     
  14. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    DungDajHjep likes this.
  15. kubci98

    kubci98

    Joined:
    Mar 15, 2013
    Posts:
    2
    Hi,
    is it possible to make NavMeshPlus work with non-grid colliders as well? (for instance having a Walls object with a composite collider) I need to have some obstacles in rotated or otherwise not fitting the grid, but even though I attach the NavMeshModifier component to any other game object (with both a BoxCollider2D and SpriteRenderer), it never actually modifies the navmesh. If it is possible, is it already implemented and I am doing something wrong, or if not, do you have any hints on how to implement this?

    Thanks
     
  16. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @kubci98,

    Yes its possible to use non-tile based objects for navmesh.
    Almost all code is moved to this single file https://github.com/h8man/NavMeshPlus/blob/master/NavMeshComponents/Scripts/NavMeshBuilder2d.cs

    As you can see:
    Code (CSharp):
    1. foreach (var modifier in grid.GetComponentsInChildren<NavMeshModifier>())
    It will collect all sources under first grid object it will find.
    So you can put all your obstacles into grid object (as child), it will not interfere with tile-map, but will be counted as a source for navmesh.

    Feature UnityEngine.AI.CollectObjects is not implemented in navmeshplus
     
    NguyenLuan likes this.
  17. kubci98

    kubci98

    Joined:
    Mar 15, 2013
    Posts:
    2
    Awesome, thank you for this.
     
  18. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    I've been spending some time trying to get the github sources to work but I'm running into the same issue as @alkaitagi. I see all the proper tagged surfaces being collected and "data" being assigned in the builder, but I don't seem to end up with any nav mesh in the scene.

    Tried on Unity 2019.3.01f

    Can anyone here verify that the stuff up on github currently works?
     
  19. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @darthbator,

    Yes, NevMeshPlus currently works and maintained.
    Did you walk through navmeshplus tutorial? Are you using NavMeshPlus 2019.3 branch with physic bake?
    Did you try demo project RedHotSweetPapper? Can you share some print screens of object with navmeshsurface2d component?

    *UP* Just tired 2019.3 final. It does bake properly.
    Don't hesitate to ask questions.
     
    Last edited: Dec 18, 2019
  20. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    Sorry I wasn't entirely clear. Before using nav mesh plus I was attempting to use the example hackweek script in the nav mesh github repo. I don't currently have my environment setup with grids so it would be more desirable for me to implement a solution like what is in the hack week repo. If I can't get that to function I'll shove my current hierarchy under a grid and use it that way with nav mesh plus until a more proper non grid dependent solution surfaces. Thanks so much!
     
    vhman likes this.
  21. Quark_Hell

    Quark_Hell

    Joined:
    Jun 11, 2019
    Posts:
    1
    Hello,how make 2D dynamic NavMesh,its impossible?
     
  22. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi,

    You can bake on runtime (if it is a case).

    There is nothing to do with NavMeshPlus, as it is "native" Unity NavMesh. You can check Pathfinding part of this forum for a solution.

    Please describe your problem in more details if it have specifics related to NavMeshPlus
     
    Last edited: Jan 1, 2020
  23. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @MelvMay,

    I got few questions from users and from myself.

    I use collider.CreateMesh(false, false) to build navmesh.
    For this particular case - NO CompositeCollider - it work well.

    upload_2020-1-8_23-27-38.png

    However collider.CreateMesh(false, false) returns null for CompositeCollider (outline or polygon):
    upload_2020-1-8_23-26-49.png

    You mentioned about this case above. But users want to know, is it possible to use CompositeCollider and get mesh from it? And how?
     
    Last edited: Jan 8, 2020
    DungDajHjep likes this.
  24. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    The CreateMesh method knows nothing about individual Unity colliders, it only sees primitive Box2D shapes and this is the level that it operates on. It works with CompositeCollider2D because that produces either Polygons (same as BoxCollider2D & PolygonCollider2D) or edges (same as EdgeCollider2D).

    All it knows are Circle, Capsule, Edge & Polygon Box2D primitives.

    Are you sure you're asking for the mesh from the CompositeCollider2D and not the TilemapCollider2D? When using a composite, the collider(s) using the composite create no shapes (as can be seen in the inspector Info roll-out of a Collider2D). All the primitive shapes are created by the composite itself which again you can see in the inspector Info roll-out.

    FYI: The shape-count of any Collider2D can be found with: https://docs.unity3d.com/ScriptReference/Collider2D-shapeCount.html
     
    Last edited: Jan 9, 2020
    vhman and DungDajHjep like this.
  25. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    201
    I think this is the crux of the problem.
     
    vhman and MelvMay like this.
  26. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    @DungDajHjep and @MelvMay
    your guess are correct.

    All I need is to add
    Code (CSharp):
    1.             if (collider.usedByComposite)
    2.             {
    3.                 collider = collider.GetComponent<CompositeCollider2D>();
    4.             }
    Thanks!
     
    DungDajHjep likes this.
  27. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    The CompositeCollider2D isn't guaranteed to be on the same GameObject nor is there any guarantee there is one as the option above is just that, an option.

    If there is a CompositeCollider2D then it must be attached to the same Rigidbody2D so you can either search for it in the parent hierarchy up to the same Rigidbody2D attached to the Collider2D or you can get the Collider2D.attachedRigidbody and then call Rigidbody2D.GetAttachedColliders on it (you can find the count here) and check if any are the CompositeCollider2D.

    What I don't understand however is why you need to do this. Doesn't the user specify which one? If so, they should specify the Composite. Alternately if you're doing all colliders on a Rigidbody2D then just use the same code above. You can even check shapeCount on each to know if you need to ignore it.

    Anyway, the logic is obviously up to you but the above should help.
     
    DungDajHjep likes this.
  28. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    User is specifying objects but not colliders. I totally missed that object, especially tilemaps one, have multiple collider components on a single object. This definitely needs some refinement from my side.
    Thank you for the tip. I'm very appreciate it.
     
    DungDajHjep and MelvMay like this.
  29. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Actually, I'm a complete idiot as I forgot that I added a property to return the CompositeCollider2D with Collider2D.composite! You can use this in conjunction with Collider2D.usedByComposite to determine if it's using a composite and what the composite component is.

    Much easier!
     
  30. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi All,

    Just merged NavMeshPlus 2019.3 with 2019.2 using UNITY_X_Y_OR_NEWER directive.
    In that way it would be less work for me with future updates.

    Also there new wiki "How To" page, opened for community modifications
     
  31. alkaitagi

    alkaitagi

    Joined:
    Dec 8, 2016
    Posts:
    87
    @vhman Thank you for your work. Having this as a package is very convenient and I can confirm it works.
     
  32. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    @vhman First, thanks :)

    Second, I've just created an issue (https://github.com/h8man/NavMeshPlus/issues/20).

    I'm seeing a bug where setting agent.velocity only works if the velocity vector has a non-zero value for x.

    Setting agent.velocity = (0,5,0) is ignored, agent.velocity = (0.0001f,5,0) works as expected. Or ... is this a Unity bug @MelvMay ?
     
  33. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Sorry but I wouldn't know, I'm not a NavMesh dev.
     
  34. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @Aurigan , very appreciate for you feedback.

    NavMeshPlus and its components (even original ones) are all about "Bake"-ing navigation mesh.
    As soon as you get correct mesh, there is nothing that NavMeshPlus and its components (even original ones) - can do for you. Sadly, but I don't have any 2D wrappers for Agent.

    I would recommend to ask a question on "Pathfinding" part of this forum and experiment with Agent in 3D space, maybe its Unity peculiarity.
     
  35. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    @vhman Thanks, I've tried a few things to work out what's actually breaking ... think I have this isolated as a Unity issue so will go bug them separately (edit - https://github.com/Unity-Technologies/NavMeshComponents/issues/149) but, for anyone curious ...

    If we open your RedHotSweetPepper sample scene, remove the NavMesh x rotation and set agent.velocity = (horizontal, 0, vertical) so that we're moving on XZ plane ... then movement works as expected. So there's something about trying to set velocity on a navmesh on an XY plane with a 0 x value that the agent doesn't like.

    I then tried making this break without anything to do with NavMeshPlus and it did. Using a 3d scene with navmesh rotated 90 degrees around x shows the same behaviour - agent.velocity works unless velocity.x is set to zero.
     
    Last edited: Jan 28, 2020
    vhman likes this.
  36. flippi273

    flippi273

    Joined:
    Nov 17, 2018
    Posts:
    24
    I wanted to try and go about baking a navmesh on a tilemap, and I stumbled upon this thread. I was wondering, based on the discussions so far, and the official release of 2019.3, is NavMeshPlus required or does it simply make it work easier? Just trying to understand if I need to install NavMeshPlus or if I can do this now with just 2019.3.

    Thanks!

    Edit: I should say the work done on NavMeshPlus looks great! How To tutorial seems pretty simple to do and the working demo is a fantastic thing to include!
     
  37. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @flippi273 ,

    NavMeshPlus in not mandatory, it just a component with simple solution. You can use this branch of NevMeshComponents as well (I didn't try it). In both cases you need to use some component to utilize NavMesh API.
     
  38. flippi273

    flippi273

    Joined:
    Nov 17, 2018
    Posts:
    24
    @vhman Thanks for the reply! I did go ahead and install NevMeshPlus and after some tweaking I've been really enjoying the results - great job!

    I do want to follow-up with a question though about how sprites are excluded when baking the navmesh. If I understand correctly from playing around and reading other posts, the outline of the sprite is used for the exclusion. Is there anyway to change this to the physics shape instead? The reason I ask is because I want my player sprite to be able to walk behind the wall like so:
    upload_2020-1-31_19-46-39.png

    With a normal sprite outline I correctly get a NavMesh like so:
    upload_2020-1-31_19-56-13.png


    But in order to do that I would like, I have to have a custom outline like so:
    upload_2020-1-31_19-47-43.png

    As you can see though, this will cut the rendering of the sprite though:
    upload_2020-1-31_19-48-16.png

    But of course, it does give me the results I want:
    upload_2020-1-31_19-49-34.png


    If I cannot use the Custom Physics Shape instead, I have two solutions that will work for me, but I'm not sure I like doing them :D

    Solution 1:
    Set the outline like I'd did in my example above, and bake the NavMesh. Then revert custom outline, but keep the bake.
    Solution 2:
    2. Create another tile map and on this layer have cut sprites and add the NavMeshModifier to this TileMap instead of the one with the full drawn sprites.

    I see both of these solutions as having cons.
    Solution 1:
    Requires me to have to flip back and forth between custom outlines. Could be a bigger problem in the event that I need to make updates after I Bake and then have to reset the outlines.

    Solution 2:
    Requires me to have extra sprites on a new tilemap just for NavMesh baking.

    Ultimately though I am leaning towards Solution 2.

    Thanks again!
     
  39. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @flippi273 ,

    I'm using "solution 2" by painting unwalkable surface with solid red colored sprite, and just disabling TileMapRenderer. But it just that is works for me.

    Fortunately you can bake physics with Unity 2019.3. Here some resources wiki, look for USED GEOMETRY
     
    flippi273 likes this.
  40. flippi273

    flippi273

    Joined:
    Nov 17, 2018
    Posts:
    24
    That's exactly what I was looking for! I must have missed that when I was reading the wiki earlier, thanks! I tried it out and everything worked as expected. I'm very happy with the flexibility and possibilities that NavMeshPlus provides!
     
  41. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @flippi273 , you didn't miss it, wiki was updated yesterday)))))
     
    flippi273 likes this.
  42. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi All, minor update.

    Now builder collects sources with respect to agent settings in modifier.

    Code (CSharp):
    1.     if (!modifier.AffectsAgentType(builder.agentID))
    2.     {
    3.           continue;
    4.     }
     
    DungDajHjep likes this.
  43. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi All,

    I'm adding some whacky transforms:
    Code (CSharp):
    1. Matrix4x4.TRS(Vector3.Scale(tilemap.GetCellCenterWorld(vec3int), builder.overrideVector), tilemap.transform.rotation, tilemap.transform.lossyScale) * tilemap.orientationMatrix * tilemap.GetTransformMatrix(vec3int);
    And still it doesn't work for Grid.cellGap :(
    upload_2020-3-14_1-25-40.png
     
  44. mathibesil

    mathibesil

    Joined:
    Nov 17, 2014
    Posts:
    2
    Hi im starting with 2d game development, can any one give me the last GitHub link for download the NavMesh extension? ty!
     
  45. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
  46. chriseborn

    chriseborn

    Joined:
    Mar 21, 2020
    Posts:
    1
    @vhman - Just wanted to say thanks for all your work.
    Saved me loads of time which I can spend on my actual game :)
     
    Biktor and vhman like this.
  47. Biktor

    Biktor

    Joined:
    Apr 10, 2014
    Posts:
    7
    @vhman - Thank you so much for your great work.
    I'm using isometric tilemaps and works perfectly :)
     
    vhman likes this.
  48. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    Hey @vhman,
    Any idea how I could implement multiple floor on navMesh, like the following?

    I would like to have this perspective on my 2d map.
    Both yellow and red lines as walkable.

    image0.jpg
     
  49. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @felipopasqual.

    You want to add depth into 2D game. There is no so simple solution. Unity components for 2D does not have Z value, and use layers instead. So naturally, we may think as we need second navmesh on different layer. Bake two meshes and add navmesh links between them.
    upload_2020-5-1_12-1-34.png

    OR, as navmesh is 3D component, you can bake on one layer, just by moving Tilemap on Z axis. And connect surfaces by links or slopes.
    upload_2020-5-1_12-20-39.png

    Both approaches have their downsides and peculiarities. I didn't make implementation of such case yet, so I'm not aware of issues you may encounter. But you can always come back to this thread.
     
    MelvMay likes this.
  50. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    Thank you @vhman
    By coincidence (or not) I was trying something similar with the approach ONE, but faced some issues with the links.

    It's creating some distortion in the movement transition, it's not smooth (feels like it was accelerating and jumping instead of moving).

    Let me try approach TWO with the Z axis.

    Thanks again.
     
Thread Status:
Not open for further replies.