Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

2D NavMesh PathFinding......

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

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

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    Sorry the double post.

    @vhman, I was able to add depth moving the Z Axis with slope :D:D

    Although, I'm having issues to move the player on Y axis (it's "stucking", not sure why but the X axis is fine).

    I've tried with the agent.destination and agent.velocity. Only got success with the transform.position (which I'm seeing as not the proper approach).
     
  2. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi,

    Use this snippet to troubleshoot.
    Code (CSharp):
    1.         public override void DebugDrawPath()
    2.         {
    3.             length = Path.GetCornersNonAlloc(waypoints);
    4.             for (int i = 1; i < length; i++)
    5.             {
    6.                 Debug.DrawLine(waypoints[i - 1], waypoints[i], Color.blue);
    7.             }
    8.             if (length > 1)
    9.             {
    10.                 Debug.DrawLine(waypoints[0], waypoints[1], Color.red);
    11.             }
    12.         }
    Also check path status
    Code (CSharp):
    1.             if (Path.status == NavMeshPathStatus.PathInvalid)
    2.             {
    3.                 Debug.Log("NavMeshPathStatus.PathInvalid");
    4.                 return false;
    5.             }
    6.             if (Path.status == NavMeshPathStatus.PathPartial)
    7.             {
    8.                 Debug.Log("NavMeshPathStatus.PathPartial");
    9.                 return false;
    10.             }
     
  3. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    I was taking a look at this the other day. NavMeshPlus us working pretty well for me but I badly want to refactor it in a way that doesn't require tilemaps. From what I can gather from what I went through the tilemaps are being used to define the boundaries of the world and then gather any objects within their bounds for inclusion into the eventual nav bake.

    So if I wanted to create an alternate surface or path within your navmeshsurface2d the main things I would need to handle is finding some way to gather scene objects and a way to define the boundaries of the world?
     
  4. vhman

    vhman

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

    The short answer is - Yes, you got it right.

    Now navmeshplus works with tilemap and should work without it. So we should consider its as an issue.

    Scene objects are collected through NavMeshModifiier marking, that can be attached to any scene object with collider or sprite. Also you can use composite collider on parent object to get mesh in one place. Though, NavMeshSurface option "CollectObjects2d m_CollectObjects" does not implemented.

    As for boundaries. They should be auto extended by this loop

    Code (CSharp):
    1.             foreach (var src in sources)
    2.             {
    3.                 switch (src.shape)
    4.                 {
    5.                     case NavMeshBuildSourceShape.Mesh:
    6. .....
    So, it should be tested. I will check it today, and I will update this post.

    *UPDATE*
    So, alternate map "extension" not by a tilemap, does work
    upload_2020-5-5_14-7-30.png

    But I found some code is bound to Grid as a root and bounds as tilemap bounds.
    I will improve it today, and update samples with non tilemap scene

    *UPDATE*
    Almost done non tiled scene bake. Few tweaks and it will be available on GitHub
    upload_2020-5-5_15-33-3.png
     
    Last edited: May 5, 2020
  5. vhman

    vhman

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

    I added and tested scenario for non-tilemap scene. If you add objects as NavMesh child and select appropriate option, you will get navmesh that does not depend on tilemap.
    upload_2020-5-5_16-42-2.png

    Also there one point in code where you can "extend" it
    Code (CSharp):
    1.         internal IEnumerable<GameObject> GetRoot()
    2.         {
    3.             switch (CollectObjects)
    4.             {
    5.                 case CollectObjects2d.Children: return new[] { parent };
    6.                 case CollectObjects2d.Volume:
    7.                 case CollectObjects2d.All:
    8.                 default:
    9.                     return new[] { GameObject.FindObjectOfType<Grid>().gameObject };
    10.             }
    11.         }
    Enjoy
     
  6. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    :) You're a gift to this community man. Thank you so much!
     
  7. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    One issue I have run into with tilemap based map generation is having navmesh generated in what I would consider "null" areas. In my game the spaces between platforms should not be transversable without a nav mesh link.

    upload_2020-5-6_1-12-46.png

    I would generally expect nav to be restricted to individual "platforms" on the tilemap. In this case would I be required to paint non "nonwalkabe" tiles in between the platforms in order to break up the generated mesh? I'm currently assuming that the space here is being defined as part of the "ground" tilemap area even though nothing is actively painted on it.
     
  8. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    darthbator likes this.
  9. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    I'm getting PathComplete and lenght = 0.
    Noticed that it's incrementing decimals during the movement on Y axis.
     
  10. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi @felipopasqual,

    Try to do it from code Agent.SetDestination(new Vector (10,10, Z_DEPENDING_ON_PLANE)) , but be sure that agent and destination is on navmesh.

    Maybe share your navigation snippet. But, after you bake navmesh, you have unity "native" component.
     
  11. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    @vhman
    The problem is really on Y axis, it's not moving up and down.

    Code (CSharp):
    1. Vector3 Direction = Vector3.zero;
    2. float Speed = 1.5f;
    3.      
    4. if (Input.GetButton("Horizontal")){
    5.     if (Input.GetAxisRaw("Horizontal") > 0)    {
    6.         Direction = Vector3.right;
    7.     }
    8.     else if (Input.GetAxisRaw("Horizontal") < 0)    {
    9.         Direction = Vector3.left;
    10.     }
    11. }
    12. else if (Input.GetButton("Vertical")){
    13.     if (Input.GetAxisRaw("Vertical") > 0)    {
    14.         Direction = Vector3.up;
    15.     }
    16.     else if (Input.GetAxisRaw("Vertical") < 0)    {
    17.         Direction = Vector3.down;
    18.     }
    19. }
    20.  
    21. if (Direction != Vector3.zero){
    22.     agent.velocity = Direction * Speed;
    23.     //agent.SetDestination(agent.transform.position + Direction);
    24. }
     
  12. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
  13. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    You rocks!
    Many thanks @vhman
     
    vhman likes this.
  14. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    Hey @vhman I have few questions.

    - How I could set to restrict agent's objects walking on top of each other?
    - If I want to create a temporary block in the navmesh, I could create an object with obstacle, so it would carve the non-walkable, right?
    - If I want to temporary walk on top of the colliders (like a flying), should I create a brand new bake and change the player navmesh at runtime?
     
  15. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi,

    1. You should have avoidance enabled for each agent, here and here.
    2. Yes, with "Carve when stationary" option enabled.
    3. You can switch agent type from "Human" to "Airborne" to traverse on different nav meshes. Or, if there is no obstacles - just disable agent.

    Just to consider. NavMeshPlus produce unity "native" navmesh. So you can brose other parts of forum for best practices.
     
  16. felipopasqual

    felipopasqual

    Joined:
    Apr 23, 2020
    Posts:
    8
    Thank you for the inputs and support!
     
  17. Vollkh

    Vollkh

    Joined:
    May 19, 2020
    Posts:
    3
    Hi,
    Im making a 2D (2D RigidBodies and 2D colliders) TopDown Shooter, and was wondering if you would suggest your solution for implementing my pathfinding, wich i want to be similiar to the pathfinding in hotline miami.
    Or, if you would recommend either restructuring my game in 3D or using A* instead, since i am not that far into my project yet anyway.

    So Basically my question is: Is there a reason why i would work with this NavMesh solution instead of A* or an actual 3D design, and what are the advantages and disadvantages.
     
    Last edited: May 25, 2020
  18. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi, @Vollkh

    NavMesh uses A* algorithm under the hood. And what NavMeshPlus does, is utilize NavMesh (A*) that is developed for 3D scene, to work with 2D components, like sprites or collider2D. NavMesh Plus produces Unity "native" NavMesh, that can utilize other Unity components. NavMesh Plus does not have any pathing algorithm implementation.

    We can compare NavMesh vs "A* Pathfinding Project" by Aron Granberg. In short "A* Pathfinding Project" is more flexible, you get the sources, it cost you money. Full features comparison is on project's page https://arongranberg.com/astar/freevspro

    Hope it helps)
     
  19. judemcenth

    judemcenth

    Joined:
    Feb 27, 2016
    Posts:
    13
    Hello. firstly I'd like to thank you a lot for this package. Its been a huge help in my project. One thing I'd really appreciate you'd add is a demo scene for the non-tilemap navmesh cause I've been having some trouble getting it to work. thank you! :)
     
    vhman likes this.
  20. Undertaker-Infinity

    Undertaker-Infinity

    Joined:
    May 2, 2014
    Posts:
    112
    Hi, I'm trying this out

    My game has layers, so I made one surface per layer (rotated -90x) as a child of the grid (which shouldn't be rotated) and set the tilemap as a child (rotated 90x). This worked fine.
    I added obstacles in the first layer and had them carve. Since they carve a volume in all surfaces and all layers, objects in one layer also carved the other layers.
    My first approach to solve this, was to set some distance between the tilemaps: I moved the grid object which contains the surface and tilemap back 5 units. Success! no longer are the obstacles carving the wrong navmesh! But if now I hit bake, the tilemap is not detected. I have to move it back to x=0 to have it detected.
    To make this work, my guess is either the obstacle code needs to change (a new obstacle2d class) to let us pick what surface/navmesh we are going to carve, or gathering needs to work outside of the origin.
    Do you think this is possible? or maybe another approach?

    A tiny issue I had starting up: In the repo, Gizmos.meta is missing. As a package, it can't be generated so Unity will abort loading the package, which defeats the manifest install method. I just manually copied the package to assets, grabbed the meta unity generated and copied it to the package folder, then deleted the copy under assets.

    Great work, by the way. This should have been made by Unity long ago, but you stepped up!
     
    vhman likes this.
  21. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi,

    Thank you for feedback. I added Gismos.meta.

    Full credits will be: Vinnie711 for Idea (first post), MelvMay (Unity staff) for Collider to Mesh API, and Unity team for implementation of navmeshsurface. I just made a repo and some basic support) I glad that people like it.

    Back to your question. NavMeshObstacle is a part unity NavMesh API, and seems it does not expose control per surface/layer. So you approach to move layers is 3d space is a workaround, if obstacles are mandatory.

    You can move tilemap on Z axis, and bake it. But you can get renderer sorting issues, see details for sorting order on unity docs.
    Or bake it flat, and move NavMesh afterward, as soon as you move navmesh away from obstacle, it would have its original shape. (hint: create component to move meshes automatically)

    Or use navmesh volume (it works per layer) and update navmesh in runtime!

    If I understood it right, it should work

    upload_2020-6-15_11-11-31.png
    upload_2020-6-15_11-11-57.png
     
  22. Undertaker-Infinity

    Undertaker-Infinity

    Joined:
    May 2, 2014
    Posts:
    112
    The workaround of baking and then moving works right now, but it seems kind of hacky.
    Navmesh works fine per layer, obstacles are the issue. I gotta have obstacles, I clear paths by destroying them. Moving the obstacles along with the navmesh and the tilemap works too, I just wish I could bake in place. It's gonna get confusing once I have more than 2 layers
     
  23. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    As I said earlier. You may want to use NavMeshVolume, and call NavMeshSurface2d.UpdateNavMesh when you need an update. Or use regular object with NavMeshModifier.
    I think you can find use case in samples.

    Code (CSharp):
    1. public AsyncOperation UpdateNavMesh(NavMeshData data)
     
  24. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    173
    Code (CSharp):
    1. internal IEnumerable<GameObject> GetRoot()
    2.         {
    3.             switch (CollectObjects)
    4.             {
    5.                 case CollectObjects2d.Children: return new[] { parent };
    6.                 case CollectObjects2d.Volume:
    7.                 case CollectObjects2d.All:
    8.                 default:
    9.                     var grids = GameObject.FindObjectsOfType<Grid>();
    10.                     List<GameObject> listObjects = new List<GameObject>();
    11.                     foreach (var item in grids)
    12.                     {
    13.                         listObjects.Add(item.gameObject);
    14.                     }
    15.                     return listObjects;
    16.             }
    17.         }
    I have edited a bit to make it support the multi grid.
    Any idea?

    P/s:
    UpdateNavMesh not work for me, but BuildNavMesh is work.

    Code (CSharp):
    1.  
    2. //yield return - here is gen map code
    3. navMeshSurface2D.BuildNavMesh();
    4. //yield return navMeshSurface2D.UpdateNavMesh(navMeshSurface2D.navMeshData);
    5. //navMeshSurface2D.navMeshData is bake data from simple map, save as assets
    6.  
     

    Attached Files:

    Last edited: Jun 25, 2020
  25. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi @DungDajHjep

    Code you showed should work for multi-grid. Its fine.

    As for UpdateNavMesh, I didn't try it for myself. I will create sample scene for UpdateNavMesh on the next week and will post an update on results.

    Thank you for feedback. I'm very appreciate every feedback I get.
     
    DungDajHjep likes this.
  26. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi @DungDajHjep , @Undertaker-Infinity

    I used next few lines to update navmesh on runtime. In small scene from demo it can be
    done every frame, but I encourage you to make some optimization suited to your project

    Code (CSharp):
    1.     public NavMeshSurface2d surface;
    2.  
    3.     // Update is called once per frame
    4.     void Update()
    5.     {
    6.         surface.UpdateNavMesh(surface.navMeshData);
    7.     }
     

    Attached Files:

    subGlitch and DungDajHjep like this.
  27. subGlitch

    subGlitch

    Joined:
    Jan 27, 2015
    Posts:
    23
    Hello, @vhman. I found your great tool this week and very excited about it!

    But, I discovered a weird behavior.

    Everything works fine. But I noticed "Failed to create agent because there is no valid NavMesh" warnings in the log, posted during agent instantiation. But agents are still instantiated and working. Than I noticed that I spawn them BEFORE BuildNavMesh() call. OK, that's makes sense...

    But after I moved code of their spawning AFTER BuildNavMesh() call everything broke. First, warnings are still remained. Second - agents are not working anymore - there are errors: ""SetDestination" can only be called on an active agent that has been placed on a NavMesh."

    Any ideas?
     
  28. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
  29. subGlitch

    subGlitch

    Joined:
    Jan 27, 2015
    Posts:
    23
    I found the problem. Turned out it was all on my side. I set transform.position to place my agents. Which is wrong. It should be done via navMeshAgent.Warp() method.

    Regarding warnings - same thing. I used Instantiate( prefab, parent ) overload and after instantiation set its position. So agent was instantiated in default position, which is Vector3.zero and which is not on the navMesh. If you want to get rid of these warnings use Instantiate( prefab, position, rotation, parent ) overload and specify correct position, so it will be instantiated on the navMesh directly.
     
    vhman likes this.
  30. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    166
    Hi and firstly thanks for the hard work and great support, really appreciated.

    It seems calling the UpdateNavMesh() once doesnt work, but calling it twice does. Not sure what is happening here.

    I had to do this;

    Code (CSharp):
    1.  
    2.     private void Update()
    3.     {
    4.         CheckToUpdateNavMesh();
    5.     }
    6.  
    7.     public void MakeWall()
    8.     {
    9.         Vector3Int tilePos = groundMap.WorldToCell(cursorPoint);
    10.         groundMap.SetTile(tilePos, WallTile);
    11.         navMeshIsDirty = true;
    12.         secondWriteDone = false;
    13.     }
    14.     public void ClearWall()
    15.     {
    16.         Vector3Int tilePos = groundMap.WorldToCell(cursorPoint);
    17.         groundMap.SetTile(tilePos, null);
    18.         navMeshIsDirty = true;
    19.         secondWriteDone = false;
    20.     }
    21.  
    22.     private void CheckToUpdateNavMesh()
    23.     {
    24.         if (navMeshIsDirty || !secondWriteDone)
    25.         {
    26.             navMesh.UpdateNavMesh(navMesh.navMeshData);
    27.             if (secondWriteDone)
    28.             {
    29.                 navMeshIsDirty = false;
    30.             }
    31.             else
    32.             {
    33.                 secondWriteDone = true;
    34.             }
    35.         }
    36.     }
    37.  
    Essentially just doing two updates with a frame draw in between. Am I missing something?

    Cheers
    Matt
     
  31. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi,

    I didn't write NavMeshSurface2d.UpdateNavMesh(). I was copied form https://github.com/Unity-Technologies/NavMeshComponents with no essential modification.

    What I can advice is to check, does new information were collected in line 205
    Code (CSharp):
    1. static private void CollectTileSources(....
     
  32. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    166
    Hi

    In NavMeshBuilder2d I'm hitting the

    Code (CSharp):
    1. private static void CollectSources(List<NavMeshBuildSource> sources, NavMeshModifier modifier, int area, NavMeshBuilder2dWrapper builder)
    method, because I'm using colliders instead of render

    Code (CSharp):
    1. if (builder.CollectGeometry == NavMeshCollectGeometry.PhysicsColliders)
    2.                     {
    3.                         CollectSources(sources, modifier, area, builder);
    4.                     }
    I'll keep digging and come back if there is anything I can add.

    Cheers
     
  33. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    DungDajHjep likes this.
  34. Ari-Levi

    Ari-Levi

    Joined:
    Dec 3, 2011
    Posts:
    15
    Hey,
    I can't manage to get the nav mesh to bake in versions post 2019.3? Is there any extra steps for these versions?
     
  35. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi, I have a project in 2019.4. There is no extra steps

    In which version of unity you have issue? Can you share any details, like scene setup, maybe few print screens?
     
  36. Ari-Levi

    Ari-Levi

    Joined:
    Dec 3, 2011
    Posts:
    15
    Sorry False alarm, Gizmos were turned off (I think this is the new default so I was confused).
     
    vhman likes this.
  37. CISAC9

    CISAC9

    Joined:
    Feb 24, 2018
    Posts:
    9
    Any confirmation of it working in Unity 2020?
    I used the instructions in the wiki to create the NavMesh using sprites instead of a tilemap.
    I keep getting the "Failed to create agent because it is not close enough to the NavMesh" warning and
    ""SetDestination" can only be called on an active agent that has been placed on a NavMesh" error.
    I baked the mesh but it doesn't show up (Gizmos are on) and all I did for the agent was place a sprite with the NavMeshAgent component in the scene and give it a script with the code you instructed as well as the line "Agent.destination = Destination.position;" (I defined a Destination transform and assign it through the inspector).
    Moving it closer to the NavMeshSurface does not fix the issue, I tried.
    Any ideas?
    Thanks.

    [EDIT]
    I got it to work. The colliders need to be set as triggers for the NavMesh to be generated and show up in the scene. I did not know this, but now if anyone has the same issue, that's probably why!
    Confirmed working in Unity 2020.1.7f1
     
    Last edited: Nov 10, 2020
  38. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hi, @CISAC9

    I'm not sure about your claims. Did you try https://github.com/h8man/RedHotSweetPepper sample project?

    Colliders will be used ony with "Use Geomentry" - "Physics Colliders" and sprite shapes only with "Use Geomentry" - "Render Meshes".

    I will check 2020.

    If there issue within the repo, I would like to fix it.

    Code (CSharp):
    1.                     if (builder.CollectGeometry == NavMeshCollectGeometry.PhysicsColliders)
    2.                     {
    3.                         CollectSources(sources, modifier, area, builder);
    4.                     }
    5.                     else
    6.                     {
    7.                         var tilemap = modifier.GetComponent<Tilemap>();
    8.                         if (tilemap != null)
    9.                         {
    10.                             CollectTileSources(sources, tilemap, area, builder);
    11.                         }
    12.                         var sprite = modifier.GetComponent<SpriteRenderer>();
    13.                         if (sprite != null)
    14.                         {
    15.                             CollectSources(sources, sprite, area, builder);
    16.                         }
    17.                     }
     
  39. CISAC9

    CISAC9

    Joined:
    Feb 24, 2018
    Posts:
    9
    I did try the sample project and got it to work there, that's how I figured out they had to be set as triggers for the mesh to be generated.
    But now that you mention it, I reopened my 2020.1.7f1 project and after changing all the colliders to not be triggers, it still works.

    Now I'm not sure what exactly fixed it...
    Probably messing with the NavMeshAgent settings (namely the Base Offset) so they'd match the ones in the Navigation tab.
    The repo is probably fine, I think it may have been user error.
     
    vhman likes this.
  40. unity_MLpMzGSSXmG6Wg

    unity_MLpMzGSSXmG6Wg

    Joined:
    May 12, 2020
    Posts:
    2
    Hey, I wanted to ask how to make two separate navmeshes? I need two different types of enemies with opposite behaviors. I was wondering how to do that because when I tried it, it gave me an error. (This is the first time I am posting a question on any unity forum so if you would like me to show you something like code or whatever can help, I'll be glad to do so).
     
  41. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hello @unity_MLpMzGSSXmG6Wg

    You need to have two Game Objects with NavMeshSurface each. Select agent type for each NavMeshSurface, bake surfaces, create your agents and use corresponding type for each. No coding required.
    upload_2020-12-26_20-3-4.png
     
    unity_MLpMzGSSXmG6Wg likes this.
  42. unity_MLpMzGSSXmG6Wg

    unity_MLpMzGSSXmG6Wg

    Joined:
    May 12, 2020
    Posts:
    2
    @vhman I am trying to make one are walkable for one agent and for another agent, that area is not walkable. For this do I use the NavMeshModifier for this?
     
  43. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Yes, NavMeshModifier has option to override area type per agent type
     
    unity_MLpMzGSSXmG6Wg likes this.
  44. IndiaVenom

    IndiaVenom

    Joined:
    Jun 16, 2015
    Posts:
    2
    Question for you guys. I'm trying to run some tests. I've made a game object with a Rigidbody 2d, Circle Collider 2D and the navmesh. All this seems to do is cause the agent to spasm. When I make the collider a trigger it works fine. I'm trying to make the surface solid though. Any idea?
     
  45. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
  46. sammarin

    sammarin

    Joined:
    Oct 14, 2019
    Posts:
    4
    Hi @vhman,
    First thanks for your great tool!

    Here is my case: I have a big 2D procedurally generated world. For the sake of performance, each chunk of this world is only one single game object that contains one big mesh with a lot of tiles:
    chunk.png
    To have obstacles (grey tiles), I programmatically add several box2D collider components at specific locations on this chunk game object.

    I woud like to integrate NavMeshPlus to add 2D pathfinding on this world (just for the 9 chunks around the player) but I don't know how to add multiple nav mesh modifers and nav mesh obstacles on one unique game object.

    Thanks!
     
    Last edited: Jan 16, 2021
  47. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hello @sammarin

    Hi, I believe something very similar here in the samples:
    https://github.com/Unity-Technologies/NavMeshComponents/tree/master/Assets/Examples/Scenes
    Particularly this scene: 4_sliding_window_infinite.unity

    And this samples https://github.com/h8man/RedHotSweetPepper shows how to work with NavMeshModifiers and 2dColliders (or Tilemaps).

    >> I programmatically add several box2D collider components at specific locations on this chunk game object.
    You can utilize CompositeCollider2d with NavMeshModifiers to cut navmesh with one object as the source

    Do not use NavMeshObstacles unless there is no other option, as they don't
    work with layer system and cut every nav mesh.
     
    sammarin likes this.
  48. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51

    The navmesh documentation identifies your problem:
    • If both NavMesh Agent and Rigidbody (non-kinematic) are active at the same time, you have race condition
      • Both components may try to move the agent at the same which leads to undefined behavior
    Why do you have a Rigidbody 2d? From the docs:

    • You don’t need to add physics colliders
      to NavMesh Agents for them to avoid each other
      • That is, the navigation system simulates agents and their reaction to obstacles and the static world. Here the static world is the baked NavMesh.

    If you need the Rigidbody 2d so other objects will bounce off your gameobject, the documentation has a solution:
    • If you want a NavMesh Agent to push around physics objects or use physics triggers:
      • Add a Collider component (if not present)
      • Add Rigidbody
        component
        • Turn on kinematic (Is Kinematic) - this is important!
        • Kinematic means that the rigid body is controlled by something else than the physics simulation
     
    vhman likes this.
  49. codxr

    codxr

    Joined:
    Jun 19, 2019
    Posts:
    11
    Hi @vhman,
    is it possible to use multiple grids?
    When I'm trying to use multiple grids, the mesh isn't baking correctly.
    Do you know a method to solve this?
     
  50. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    333
    Hello @codxr

    NavMeshPlus does not support multiple grids https://github.com/h8man/NavMeshPlus/issues/29
    But it is One Line to enable it, check DiegoMartinezCela solution.

    Code (CSharp):
    1.             default:
    2.                 List<GameObject> grids = new List<GameObject>();
    3.                 foreach(Grid g in GameObject.FindObjectsOfType<Grid>()) {
    4.                     grids.Add(g.gameObject);
    5.                 }
    6.                 return grids.ToArray(); // Returns all gameObjects with a Grid
    I didn't add it to repository, cause we need implementation for Collect Objects option,
    I cannot find a time to make it properly.
     
Thread Status:
Not open for further replies.