Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

2D NavMesh Plus - Components and Extensions System

Discussion in '2D' started by vhman, May 11, 2022.

  1. AndyBlock

    AndyBlock

    Joined:
    Oct 12, 2016
    Posts:
    35
    This became a lot easier to debug once I could see the NavMesh, and was a silly error on my part. Nothing to see here!

    I discovered in the Unity Docs for the CreateMesh method that the EdgeCollider2d will create a mesh if it has an edgeRadius set, so I was able to use that for my purposes... so I'm all good to go now :)
     
  2. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Hello, I didn't check Unity 2023 yet, I will do it on weekend. Thank you for a feedback.
     
  3. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Code (CSharp):
    1. I discovered in the Unity Docs for the CreateMesh method that the EdgeCollider2d will create a mesh if it has an edgeRadius set, so I was able to use that for my purposes... so I'm all good to go now :)
    this should be added to my wiki)
     
  4. sammarin

    sammarin

    Joined:
    Oct 14, 2019
    Posts:
    7
    Hello,

    Is it possible to have different navigation modifiers on different tiles of the same tilemap?
    My use case: I have a world (a tilemap) with different destructible obstacles tiles (wood, stone, iron, ...) and I would like to apply different area types (walkable, easily destructible, moderately destructible, ...) on tiles of a same tilemap in order to guide enemies in a intelligent way. For example the enemy will prefer to break the wood tiles to reach the player instead of making a big detour using a walkable area to reach the player because it is less costly for him to break these wood tiles.

    The easy workaround is to have one tilemap by type of obstacles but I'm afraid that could cause performance issues to have a lot of tilemaps.

    Thanks!
    Sam
     
  5. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Hi,

    There undocumented NavMeshModifierTilemap.cs that can override area by tile. But it is not well explored, I would say. It has interface where you can return area type per tile. That is exactly what you want.

    Ill try to create a demo scene for. But no promises when.
     
    sammarin likes this.
  6. sammarin

    sammarin

    Joined:
    Oct 14, 2019
    Posts:
    7
    Thanks a lot!
    Regards,
    Sam
     
  7. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @sammarin I've updated navmeshplus to 2022 (I didn't bother with backward combability, also I have zero clue on Editors magic lines that Unity's uses). So test it and reply if it works.

    UPDATE
    @AndyBlock I tagged wrong user name. Please check update
     
    Last edited: Apr 16, 2024
  8. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @sammarin new scene Assets/Scenes/SampleSceneTilemapModifier added. Please provide your feedback
     
  9. sammarin

    sammarin

    Joined:
    Oct 14, 2019
    Posts:
    7
    I checked the sample scene and it seems we can have different tile modifiers on different tiles of a single tilemap!
    Great job!
    Thanks for that!
     
  10. Clamoxyl

    Clamoxyl

    Joined:
    Mar 4, 2015
    Posts:
    8
    Hi!
    I'm setting a navmesh for the first time and I can't figure out how to reduce the "margin" between my tiles colliders and the actual navmesh.

    I'd like to reduce the space marked with blue arrows in the image below :)
    upload_2024-4-19_12-37-33.png

    Thanks a lot @vhman for creating this.
     
  11. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @Clamoxyl
    FAQ:
    Q: How to change NavMesh padding?
    A: Navmesh draws where the center of the Agent can reach, not his edge. Reduce Agent's radius to get navmesh closer to the edge. Bake again to see results.

    Q: How can I connect two NavMeshes together or point to the agent that there is a "jump" between?
    A: With NavMeshLinks you can connect NavMeshes or place triggers for certain actions like: jump, open a door.

    Q: Can I make a passage that is blocked to be walkable after certain action?
    A: Yes, you can update mesh at runtime, or use NavMeshObstacle
     
  12. fipps129

    fipps129

    Joined:
    Aug 18, 2012
    Posts:
    1
    Hi!
    I've been trying to set up a "hole" or "pitfall" trap, that the agent avoids, but can still be "pushed" into. I can create a Navigation Area that has a high cost, but the agents sprite overlaps the area, even though the center of it never enters.
    If I increase the area so that it expands past the "hole" they move around it realistically, but the player (target) can move into the area, so they naturally move into and over the high cost Navigation area, since its the only way to the player.
    Increasing the agents radius doesn't effect how close they move to the "hole"
    Everything else has worked perfectly, and I am really happy with how well it works!
    Is there something I'm missing or misunderstand?
     
  13. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @fipps129

    There a lot of options and I cannot say what best works for you:
    1) Check or probe path - you can check if path is over "hole" by NavMeshAgent.Raycast or NavMesh.Raycast and apply extra logic, like make player unreachable
    2) Carve a hole in nav mesh to avoid at any cost, override Agent.nextPosition when it pushed, or disable agent agent.updatePostition on such event
    3) bake "hole" as separate mesh and introduce navmesh links to trigger "jumping off"

     
    fipps129 likes this.
  14. Clamoxyl

    Clamoxyl

    Joined:
    Mar 4, 2015
    Posts:
    8
    Thanks @vhman !

    I managed to improve the padding problem by reducing the agent size, although it won't let me use a value under 0.05 and I had to modify the voxel size, as it depends on agent size somehow.

    Any ideas on how to manage my navmesh to go even closer to the colliders seen in this capture?

    upload_2024-4-22_10-4-1.png

    Thanks again!
     

    Attached Files:

  15. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @Clamoxyl How wide your character? As I said, it is a center of Agent. Center has 0 thickness. On image you provided Agent is free to go onto stairs
     
  16. Clamoxyl

    Clamoxyl

    Joined:
    Mar 4, 2015
    Posts:
    8
    Yes, my agent can go through the stairs freely, it is just that I want to make the navmesh as close to my collisions as possible, as I am already taking characters (player and agents) width into account when I set my collisions. Is there a way to make the navmesh as close as possible to the existing collisions? In my mind this would be achieved by a 0 agent size, but the system forbids that.

    I know that with the provided artwork, if the navmesh padding was reduced, the agents would appear to go over the stairs if they could walk too close to the collision, but this artwork is just a placeholder. I would prefer to have the navmesh as tightly adjusted to the collisions as possible.
     
  17. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
  18. Clamoxyl

    Clamoxyl

    Joined:
    Mar 4, 2015
    Posts:
    8
    @vhman Thanks so much! I really appreciate your help. I'll have a look at both approaches, although I may end up modifiying the collision size when our artist produces the final art, seeing that Unity is a fan of padding hehe.
    Thanks again!
     
  19. VirtualSUN

    VirtualSUN

    Joined:
    Jan 29, 2013
    Posts:
    60
    Good day.
    I'm using Unity 2021.3.22f1 and it shows me an error when I select an object from the "Navigation Surface":
    upload_2024-4-26_14-27-48.png
    It doesn't seem to affect performance.

    For now, I decided to comment on the following in the "NavMeshSurfaceEditor" script:
    upload_2024-4-26_14-30-30.png

    Can it somehow affect further workability?
     
  20. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    VirtualSUN likes this.
  21. Edenfall_

    Edenfall_

    Joined:
    May 1, 2023
    Posts:
    1
    Hello,
    I'm currently working on a top view project and I have to rotate the characters to make it look forward while moving. Is there a way to make it with the NavMeshPlus system ?
     
  22. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Edenfall_ likes this.
  23. Oliver_WalkTheFrog

    Oliver_WalkTheFrog

    Joined:
    Feb 6, 2024
    Posts:
    6
    Hey, thank you for the work you have done with the 2D nav mesh. I am currently using it and running into a small problem. The agent slows down massively when approaching a Navigation Link and then stopping before completing the jump. To synch the agent with the jumping animation I use _agent.isOnOffMeshLink, which is the intended way, right? Right now it looks unnatural that the agent takes so much time. I played around with different Speed/Acceleration values but that doesn't seem to change anything.

    The only thing making it better is setting a very high Obstacle avoidance radius but that makes the agent also jump much earlier then intended. I tried googling but I only get 10 year old tutorials that have not been very helpful. Unfortunately there is no property of the agent that could tell me if the next node of its path is an OffMeshLink, otherwise I could just check for the distance and start jumping when it makes sense without relying on _agent.IsOnOffMeshLink. :(

    EDIT: Ok after taking a closer look at the OffMeshLink I found the solution: In my case the link was orthogonal to the NavMesh, which means that only 1 point was connecting the link on each side. This caused the problem. I rotated it correctly and now everything is working as intended. I'll leave that here for anybody else.
     
    Last edited: Apr 30, 2024
  24. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Oliver_WalkTheFrog likes this.
  25. luamct

    luamct

    Joined:
    Jul 4, 2020
    Posts:
    4
    First, thanks a lot for all your work on this component @vhman and other contributors!

    I couldn't figure out how to use tilemaps + custom physics shapes. Here's my setup from a clean scene:

    1. Create NavMesh object on scene, add Navigation Surface component and NavigationCollectSources2D, set Use Geometry = Physics Colliders
    2. Go into Sprite Editor and adjust custom physics shape for some tiles
    3. Create Palette from tiles (make sure ColliderType is Sprite for all tiles)
    4. Create Tilemap on scene, add Navigation Modifier TileMap component, and add tiles as Not Walkable
    5. Bake!

    What happens is that the mesh disconsiders the Not Walkable tiles completely (check images pls). If I set Use Geometry = Render Meshes, than it bakes as if the entire tile is not walkable, which is not what I'm going for.

    Am I missing something here?

    custom-physics-shape.png

    use-geometry=colliders.png

    use-geometry=render-meshes.png
     
  26. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Hi,

    You don't need ModifierTilemap. I know its not documented, but it used only for SpriteMesh, as there no sense to override by tile with composite colliders (maybe I'm wrong).

    You need to mark your composite collider as non-walkable, so use NavigationModifier only.

    Code (CSharp):
    1.             if (builder.CollectGeometry == NavMeshCollectGeometry.PhysicsColliders)
    2.             {
    3.                 var collider = modifier.GetComponent<Collider2D>();
    4.                 if (collider != null)
    5.                 {
    6.                     CollectSources(sources, collider, area, builder);
    7.                 }
    8.             }
    9.             else
    10.             {
    11.                 var tilemap = modifier.GetComponent<Tilemap>();
    12.                 if (tilemap != null)
    13.                 {
    14.                     CollectTileSources(sources, tilemap, area, builder);
    15.                 }
    16.                 var sprite = modifier.GetComponent<SpriteRenderer>();
    17.                 if (sprite != null)
    18.                 {
    19.                     CollectSources(sources, sprite, area, builder);
    20.                 }
    21.             }
     
    luamct likes this.
  27. Oliver_WalkTheFrog

    Oliver_WalkTheFrog

    Joined:
    Feb 6, 2024
    Posts:
    6
    Yes. I uploaded an image to demonstrate what was happening. I have to rotate the links by 90 degrees to align them with the 2D plane of the nav mesh. I assume this is because they are not children of the navigation surface-gameobject.

    Now I have two more questions:
    1) What is the intended way to disable debugging? Right now I get messeges like "Sources 2". Is there a proper way besides just deleting the relevant lines in the script?
    2) Debug.Log(navAgent.currentOffMeshLinkData.offMeshLink.area) causes errors. What is the correct way to get offMeshLinkData?

    Thanks a lot!
     

    Attached Files:

  28. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    1) There is a filed, but it missing from inspector. Just A bug, Ill fix it
    [SerializeField]
    bool m_HideEditorLogs;
    2)Debug.Log(navAgent.currentOffMeshLinkData.offMeshLink.area) if will be null all the time, expect the exact time when you traversing link. It is described in a video. Also scripts for that video are on github. Did I answer your question?
     
    Oliver_WalkTheFrog likes this.
  29. Oliver_WalkTheFrog

    Oliver_WalkTheFrog

    Joined:
    Feb 6, 2024
    Posts:
    6
    Great! It did not answer the question but after searching the youtube channel you linked (thanks!) I found a video that explained exactly what I needed: A way to tell what kind of link is being traversed for picking the right animation (it's this video if anybody else needs it:
    ).
     
    vhman likes this.
  30. rpoboBLLjuk

    rpoboBLLjuk

    Joined:
    Dec 18, 2022
    Posts:
    6
    Hi all. Why is NavMesh never displayed when "Only Selected" is selected? It's just that the eternal display of NavMesh does not suit me.. upload_2024-5-14_20-32-54.png
     
  31. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    What version of Unity? I might be a bug
     
  32. rpoboBLLjuk

    rpoboBLLjuk

    Joined:
    Dec 18, 2022
    Posts:
    6
  33. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Oliver_WalkTheFrog likes this.
  34. rpoboBLLjuk

    rpoboBLLjuk

    Joined:
    Dec 18, 2022
    Posts:
    6
    @vhman, do you have any ideas about my problem?
     
  35. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
  36. rpoboBLLjuk

    rpoboBLLjuk

    Joined:
    Dec 18, 2022
    Posts:
    6
    @vhman, I opened this project under my version 2022.3.9, and it really worked. Can there be any updates in it that are not presented in NavMeshPlus on GH?
     
  37. rpoboBLLjuk

    rpoboBLLjuk

    Joined:
    Dec 18, 2022
    Posts:
    6
  38. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Identical. Do you have Navigation AI Package?
    upload_2024-5-20_19-37-35.png
     
  39. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    68
    Exactly living this problem right now. I even looked at their samples. Doing exactly same thing but it does not generates anything for my Tilemap.

    Only 3D colliders and renderers gets in count while baking. How will i tell the baker "My project is 2D! Use Box Collider 2D instead of Box Collider!!!!!"

    Fixed it by re-openning the project. Your initialization system seems broken.
     
    Last edited: May 21, 2024
  40. rpoboBLLjuk

    rpoboBLLjuk

    Joined:
    Dec 18, 2022
    Posts:
    6
    @vhman, It seems that there are both folders...
     

    Attached Files:

  41. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
  42. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    If you want only boxes use Override By Grid checkbox. Every tile with by threated as box. Yuo can also supply custom shapes if you want.