Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

2D NavMesh Plus - Components and Extensions System

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

  1. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    yes, this is undocumented Unity feature, and its not implemented yet.
     
  2. jecky111

    jecky111

    Joined:
    Jun 11, 2016
    Posts:
    18
    it would be nice to update the component names in the pdf documentation, would have saved me a few hours. anyway great asset, thanks!
     
  3. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Hello All,

    Added NavMeshLink sample into Demo project with "infinity runner" scene, where navmeshes are dynamically placed at runtime.

    Special thanks to LlamAcademy for repo with code samples on navmesh links.
     
  4. CrAzYpotpie

    CrAzYpotpie

    Joined:
    Sep 24, 2020
    Posts:
    11
    Been using this since around February for my sidescroller platforming game, and it has been invaluable to me. Thank you for sharing it with us. I had designed an AI system that was purely based on environment for my physical "motor" based NPC's (grounded NPCs that can walk, jump, etc,) and when it came time to implement a system for floating/flying NPC's that could navigate the environment, I started to look into Unity's existing systems to handle this, and was amazed that their navigation package was 3D only. That research led me to your package, and saved me a large amount of time.

    The only negatives I can say about this are all related to Unity's navigation package itself (mostly due to it not being designed with 2D in mind,) and the hacky "-89.98" x rotation which can sometimes result in the AI's being offset a tiny bit from 0 on the Z axis, but luckily I can work around that.

    Thank you again!
     
    vhman likes this.
  5. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    That is industry standard to not have decent 2d navigation support out of box. I've looked into few other engines)
     
  6. andreasambrusg

    andreasambrusg

    Joined:
    Sep 1, 2023
    Posts:
    1
    Hello guys i need some help with navMesh....

    ich made everything like in the tutorial described but when baking it says Sources 0 eventhough i have a obstacle
    upload_2023-10-12_1-38-5.png

    Further more i create my enemies at random locations on the screen (wave game like) but then i get the error that they are not placed on a navmesh this is the nav mesh agent script in bolt. This script has every enemie that spawns:
    upload_2023-10-12_1-41-25.png
    upload_2023-10-12_1-39-48.png

    btw im on 2021.2.13f1
     
  7. pepsi4

    pepsi4

    Joined:
    Mar 1, 2016
    Posts:
    8
    Can I use this to create a pathfind-system in platformer game?
     
  8. dannyalgorithmic

    dannyalgorithmic

    Joined:
    Jul 22, 2018
    Posts:
    100
  9. pepsi4

    pepsi4

    Joined:
    Mar 1, 2016
    Posts:
    8
    @dannyalgorithmic Thank you for your answer. Yeah, I saw PathBerserker2D, but I'm looking into free solutions right now.
     
  10. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @pepsi4 No, its not for 2d platformers, not out of box.
    If regular NavMesh can be used for 3d platforming, than it can be used for 2d, but only by introducing 2.5d

    Does this makes sense for you?
     
  11. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @andreasambrusg If sources 0, that mean you don't have nav mesh modifiers. NavMesh modifiers are mandatory.
    But it hard to tell about details in your setup. Start with something simple and get feel of it.
     
  12. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    58
    I noticed if I change radius here
    navagent.png

    then Navigation Surface component takes noticeably longer to bake. It seems like it only happens if I set radius to something smaller, like 0.2.

    It doesn't happen if I use UpdateNavMesh, only with BuildNavMesh (orBuildNavMeshAsync) or when baking in the inspector. Is this a bug?
     
  13. pepsi4

    pepsi4

    Joined:
    Mar 1, 2016
    Posts:
    8
    Not sure if I could understand how to do it with NavMesh.
    Right now I`m trying to build the platformer pathfind system based on the A* pathfind project (free version).
     
  14. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @DrVanillaCookie
    First - baking and pathfinding is a Unity native features. NavMeshPlus provides only interface for sources collection.
    UpdateNavMesh take less, cause it just un update, it has some optimizations.
    BuildNavMesh - I don't know, but seems with smaller agent you have more "voxels" to cover. There some dependency in advanced settings.
     
    DrVanillaCookie likes this.
  15. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    58
    You are correct, on Unity 2022.3.9f1 both NavigationSurface and NavMeshSurface take longer to bake smaller radius. 0.05 takes quite a while. I did some more testing on 2021.1.20f1 (with older NavMeshPlus) and it baked almost instantly. Oh well.
     
  16. hoyoyo80

    hoyoyo80

    Joined:
    Jan 31, 2018
    Posts:
    58
    I remember trying this on unity 2021 and work fine. But today when i tried again with 2022.3.15 this error come out and the surface wont bake:
    NevMeshExtension requires a NavMeshSurface component
    UnityEngine.Debug:LogError (object)
    NavMeshPlus.Extensions.NavMeshExtension:ConnectToVcam (bool) (at Assets/NavMeshPlus-master/NavMeshComponents/Scripts/NavMeshExtension.cs:48)
    NavMeshPlus.Extensions.NavMeshExtension:Awake () (at Assets/NavMeshPlus-master/NavMeshComponents/Scripts/NavMeshExtension.cs:28)
     
  17. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Hello,

    You used wrong NavMesh. Unity has its own implementation of NavMeshSurface. New naming is "Navigation" Surface.
    You can have NavSurface from Unity and NavMeshPlus from h8man simultaneously, just keep your eye on naming
     
  18. Krosenut

    Krosenut

    Joined:
    Jun 1, 2023
    Posts:
    20
    Hi, I'm having trouble with using navmeshagents on a tilemap. I successfully implemented runtime navmesh and agents previously, but it was a couple of weeks ago and maybe I'm missing something. So I have a tilemap with a baked nav surface that is set to use physics colliders. The player controls a tank, and I placed an allied and an enemy tank on the map. Allied tank is supposed to follow the player, and an enemy tank is supposed to chase him when detected. Neither of tanks move, but they do turn towards the player, and NavMeshAgent.SetDestination is being hit in the debugger.


     
  19. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Hello,

    From screen shots I only see z=-1, I don't know why you need that. But I think you to look at your agent script.
    Check if agent "touches" navmesh first. Use sample scene to with hardcoded set destination to see what agent is doing, like speed and etc.
     
  20. hoyoyo80

    hoyoyo80

    Joined:
    Jan 31, 2018
    Posts:
    58
    Thanks for the reply. now i replace all all component that involve with "navigation" naming but it still wont bake
     
  21. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    what does console say's?
     
  22. Krosenut

    Krosenut

    Joined:
    Jun 1, 2023
    Posts:
    20
    What is the reason of gameobjects teleporting sometimes when colliding with something?
     
  23. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    What do you mean "colliding"? NavMesh agents should not use physics colliders as Navigation is not compatible with physics. You should use only navigation or physics in the same time. Or at least physics cinematic, or update agent manually.
     
  24. Krosenut

    Krosenut

    Joined:
    Jun 1, 2023
    Posts:
    20
    So you are saying I can't have game object with navmesh agent and collider components? What can I do instead if I need to detect collisions of moving objects?
    I don't understand why objects teleport:
     
    Last edited: Jan 14, 2024
  25. mmsdocs

    mmsdocs

    Joined:
    Jan 6, 2023
    Posts:
    1
    This component isn't working on Unity 2022.3.15f1. When I bake the navmesh, it just creates the extents and don't show navmesh surface.
     
  26. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Ill check
     
  27. vhman

    vhman

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

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Last edited: Jan 23, 2024
  29. MonkeDev69

    MonkeDev69

    Joined:
    Apr 19, 2020
    Posts:
    3
    Hello, how do you make the agent's path more straight on corners (like making the agent move center of the path?), currently this is the path of the agent, it's not center and moves very close to the wall, and the corners are curved
    upload_2024-1-24_15-45-17.png
    upload_2024-1-24_15-47-19.png

    This is what I want to achieve, straight and linear paths, corners, thanks
    upload_2024-1-24_15-46-42.png
     
  30. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Only by custom code with NavMesh static classes. NavMesh Agent is Unity native component with very little customization to achieve what you want.

    Keep in mind that NavMeshPlus responsible for baking navmesh only

    Also, I see that its working with 2022.3.x, so I have no idea about previous posts about issues in never version of unity
     
    MonkeDev69 likes this.
  31. K-bomb07

    K-bomb07

    Joined:
    Mar 5, 2019
    Posts:
    7
    Hi, in Unity, I am trying to create a 2D top down shooter and I would like to create an enemy pathfinding AI for it. Every individual enemy will have their own different obstacles at different times that will be defined by an integer. Is there a way to use navmeshplus to allow every agent to have different walkable and non-walkable surfaces without interfering with other agents?
     
  32. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    Yes, you must bake nav mesh per agent type. So just create multiple Agent types.
     
  33. artur18kravchak

    artur18kravchak

    Joined:
    Oct 5, 2023
    Posts:
    1
    Hi, is there a way to place objects without updating the entire navmesh? I'm making a sandbox game and this is very important to me.
     
  34. Humusk

    Humusk

    Joined:
    Jan 8, 2021
    Posts:
    6
    Hi ya'll,

    I'm trying to bake during runtime following the player around, as I only want to "move" stuff that it's close to it, I'm trying something very basic, but I'm totally failing and I don't really get why. I mean, everything seems ok for something that short, but it just doesnt' work at all (if baking manually from Unity Editor everything works correctly, so it's my code 100% sure).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using NavMeshPlus.Components;
    5. using NavMeshPlus.Extensions;
    6. using UnityEngine.AI;
    7.  
    8. public class NavMeshManager : MonoBehaviour
    9. {
    10.     [SerializeField] private NavMeshSurface Surface2D;
    11.     [SerializeField] private CollectSources2d collectSources2D;
    12.     [SerializeField] private float UpdateRate = 0.1f;
    13.     [SerializeField] private float MovementThreshold = 3;
    14.     [SerializeField] private GameObject Player;
    15.     [SerializeField] private Vector3 NavMeshSize = new Vector3(20, 20, 20);
    16.     private Vector3 WorldAnchor;
    17.     private List<NavMeshBuildSource> Sources = new List<NavMeshBuildSource>();
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         Surface2D = gameObject.GetComponent<NavMeshSurface>();
    23.         collectSources2D = gameObject.GetComponent<CollectSources2d>();
    24.         BuildNavMesh();
    25.         StartCoroutine(CheckPlayerMovement());
    26.     }
    27.  
    28.     private IEnumerator CheckPlayerMovement()
    29.     {
    30.         WaitForSeconds Wait = new WaitForSeconds(UpdateRate);
    31.  
    32.         while (true)
    33.         {
    34.             if (Vector3.Distance(WorldAnchor, Player.transform.position) > MovementThreshold)
    35.             {
    36.                 BuildNavMesh();
    37.                 WorldAnchor = Player.transform.position;
    38.             }
    39.  
    40.             yield return Wait;
    41.         }
    42.     }
    43.  
    44.     private void BuildNavMesh()
    45.     {
    46.         Bounds navMeshBounds = new Bounds(Player.transform.position, NavMeshSize);
    47.  
    48.         NavMeshBuilderState navMeshBuilderState = new NavMeshBuilderState();
    49.         navMeshBuilderState.worldToLocal = Player.transform.worldToLocalMatrix;
    50.         navMeshBuilderState.worldBounds = navMeshBounds;
    51.         //navMeshBuilderState.roots = GetRootGameObjects();
    52.  
    53.         collectSources2D.CollectSources(Surface2D, Sources, navMeshBuilderState);
    54.  
    55.         //Surface2D.UpdateNavMesh(Surface2D.navMeshData);
    56.         NavMeshBuilder.UpdateNavMeshDataAsync(Surface2D.navMeshData, Surface2D.GetBuildSettings(), Sources, navMeshBounds);
    57.  
    58.  
    59.     }
    60. }
    61.  
    Just in case, the photo of my editor:
    upload_2024-2-11_1-3-9.png

    Thanks!
     
  35. vhman

    vhman

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

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @Humusk Never used 2d navmesh components outside of NavMeshPlus, very interesting case)

    Unfortunately, I cannot see that is going wrong. Code provided by you makes sense, just debug it. Compare what you have in "Unity Editor" and in your script.
     
  37. Humusk

    Humusk

    Joined:
    Jan 8, 2021
    Posts:
    6
    Hi vhman, still debuging, I've managed to make it work like this:
    Code (CSharp):
    1. private void BuildNavMesh()
    2.     {
    3.         Bounds navMeshBounds = new Bounds(Player.transform.position, NavMeshSize);
    4.         Debug.Log(navMeshBounds.center);
    5.  
    6.         NavMeshBuilderState navMeshBuilderState = new NavMeshBuilderState();
    7.         collectSources2D.CollectSources(Surface2D, Sources, navMeshBuilderState);
    8.         NavMeshBuilder.UpdateNavMeshDataAsync(Surface2D.navMeshData, Surface2D.GetBuildSettings(), Sources, navMeshBounds);
    9. }
    The problem I'm having right now is that the navmesh is only moving with the player in the X axis, the Y axis is stuck at the transform of the GameObject that is baking with this script & that also have the surface2d, etc...

    Also really weird, but well...let's see if I find it

    Edit: I found the problem, the y and z axis had to be swapped. The system kinda works now, but sadly my player is dragging the enemies around when they reach the border of the baked zone, I still haven't debugged that, but probably some problem related to the way I'm assigning movement to enemies with the SetDestination function.
     
    Last edited: Feb 13, 2024
  38. Fthe

    Fthe

    Joined:
    Mar 13, 2014
    Posts:
    1
    Hi, sharing some experience with using 2022.3.*, In the AI Navigation deselect Show Only Selected and select Show NavMesh, the bake navmesh will appe thingnottodo.png ar
     
    Last edited: Feb 16, 2024
    DrVanillaCookie likes this.
  39. K-bomb07

    K-bomb07

    Joined:
    Mar 5, 2019
    Posts:
    7
    Hi, is there any other way to do this besides agent types? I am going to have a lot of possible integers, and I will need to make a very high amount of agent types if I take that approach.
     
  40. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @K-bomb07

    I don't know what You are trying to achieve. With navmesh you have navmesh data, it contains agent type and 32 layers, you can probe any point on surface. With some algorithms and clever design you can create dynamic obstacles per "individual enemy" - but it is out of my scope where I can help.

    Unity's navmesh is a native component, check AI Pahtfinding part of this forum.

    Also You can check A Star https://arongranberg.com/astar/ for other options.
     
  41. K-bomb07

    K-bomb07

    Joined:
    Mar 5, 2019
    Posts:
    7
    Ok. I think I have devised an alternate strategy within the scope of NavMesh. I just require assistance with one issue. I have used this code to generate multiple Navigation surfaces and assign an AgentTypeId to each of them.
    Code (CSharp):
    1.  
    2.  public int highestFloor;
    3.     public GameObject rootPrefab;
    4.     //A prefab that contains a navigation surface and a navigation collectSources2d component
    5.  
    6.     public List<FloorColliderIdentifier> FCL = new List<FloorColliderIdentifier>();
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         highestFloor = 0;
    11.         Invoke("assignFCL", 0.2f);
    12.     }
    13.  
    14.     void assignFCL()
    15.     {
    16.         FloorColliderIdentifier[] allFCL = FindObjectsOfType<FloorColliderIdentifier>();
    17.      
    18.         for(int i = 0; i < allFCL.Length; i++)
    19.         {
    20.             FCL.Add(allFCL[i]);
    21.             if(allFCL[i].representingFloor > highestFloor)
    22.             {
    23.                 highestFloor = allFCL[i].representingFloor;
    24.             }
    25.         }
    26.  
    27.         bakeTheGrids();
    28.     }
    29.  
    30.     void bakeTheGrids()
    31.     {
    32.         for(int i = 0; i < highestFloor + 1; i++)
    33.         {
    34.             GameObject navMeshRoot = Instantiate(rootPrefab, Vector3.zero, rootPrefab.transform.rotation);
    35.             navMeshRoot.name = "Floor " + i.ToString() + " NavMesh Root";
    36.             navMeshRoot.GetComponent<NavMeshSurface>().hideEditorLogs = true;
    37.  
    38.  
    39.             for (int k = 0; k < FCL.Count; k++)
    40.             {
    41.                 if (FCL[k].representingFloor <= i || FCL[k].representingFloor == i + 1)
    42.                 {
    43.                     Debug.Log(FCL[k].name + " is walkable for " + navMeshRoot.name);
    44.                     FCL[k].GetComponent<NavMeshModifier>().area = 0; //WALKABLE
    45.                 }
    46.                 else
    47.                 {
    48.                     Debug.Log(FCL[k].name + " is not walkable for " + navMeshRoot.name);
    49.                     FCL[k].GetComponent<NavMeshModifier>().area = 1; //NOT WALKABLE
    50.                 }
    51.             }
    52.             navMeshRoot.GetComponent<NavMeshSurface>().agentTypeID = i;
    53.             navMeshRoot.GetComponent<NavMeshSurface>().BuildNavMesh();
    54.         }
    55.     }
    But, I am having an issue on line 50 of the code, where instead of assigning the agentTypeID, I get a message that looks like this on every NavigationSurface I create. What does this message mean and how can I assign the variable correctly?
    upload_2024-3-9_9-26-48.png

    upload_2024-3-9_9-33-26.png
     

    Attached Files:

  42. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    @K-bomb07 I see nothing wrong with code you have. Try to debug your the script
     
  43. vamosfa

    vamosfa

    Joined:
    May 15, 2016
    Posts:
    65
    Really thanks mate!!

    I wasted almost a full day trying to figure out why there was no possibility of NavMeshSurface recognition...

    Maybe these components should have different names than the default ones of Unity...Just my opinion

    Thanks a lot to the asset creator

    Unity version 2022.3
     
  44. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    373
    I was not expecting Unity will release their components as package. It was convenient to reuse Unity code base at that moment.
     
    vamosfa likes this.
  45. latricemillicent

    latricemillicent

    Joined:
    Mar 14, 2024
    Posts:
    1
    Thank You!
     
  46. SpicyDinosaur

    SpicyDinosaur

    Joined:
    Oct 25, 2019
    Posts:
    5
    I'm having problems, I think because of AI Navigation that's been discussed above, but when I try to drag the scripts to the appropriate game object, it returns this error:



    I used the git link to install the package and updated it,though it didn't need it. Everything is there, the problem also seems to involve the AI Navigation package, but uninstalling that package causes other problems and I still get the above error trying to drag in the scripts.
     
  47. vamosfa

    vamosfa

    Joined:
    May 15, 2016
    Posts:
    65
    Hi!

    Did you try another installation method? Instead of using the link:
    . Drag the NavMeshComponents assets into the Assets folder. I used this one from the last package two days ago and it works
     
  48. SpicyDinosaur

    SpicyDinosaur

    Joined:
    Oct 25, 2019
    Posts:
    5
    I actually solved it. Glad you replied to remind me to post what I did.

    I had to update the editor to the newest LTS version. I hadn't wanted to do this anymore since it can cause conflicts with the game, obviously, but so far there are no issues and the nav mesh is working once more.
     
    vamosfa likes this.
  49. AndyBlock

    AndyBlock

    Joined:
    Oct 12, 2016
    Posts:
    35
    First, thanks for creating this asset - it will be very helpful if I can get it to work for my case! I have a couple of issues, though.

    My primary issue is that I have problems with not being able to see the baked navmesh. I am using Unity 2023.2.16f1. I tried following the readme instructions, and various tutorials but nothing seemed to work - when I hit 'Bake' nothing would show up. I checked Gizmos were enabled, navigation tab showing, Navigation Surface selected, but no joy.

    Then I tried the RedHotSweetPepper sample in 2021.3.29f1, and that worked fine. However, when I converted that to 2023.2.16f1, it also no longer worked.

    I have also confirmed that the problem is not just that nav mesh isn't showing in general by creating a 'NavMeshSurface' (from Unity's navigation package), setting up a 3d cube and a plane with that, and baking that, and that shows up fine.

    Yet apart from not showing the navmesh in the inspector, everything seems to work fine - paths are found by agents and obstacles are avoided correctly. So I'm at a loss as to what the cause could be. Maybe it just isn't recognising 'Navigation Surface' as being something it needs to render NavMesh gizmos for in Unity 2023 for some reason?

    My second problem is that in my "real" scene, an agent that "should" be successfully placed on my NavMesh is generating an error ""SetDestination" can only be called on an active agent that has been placed on a NavMesh.". I'm guessing this is simply an issue with the NavMesh that has been generated, but it's hard to figure out what when I can't see the NavMesh :) .

    A final issue I have is that I am using EdgeCollider2d in places - but it looks like that won't work because CreateMesh returns null for those. It would be nice to treate those as a 'very thin area' ideally. If anyone has suggestions for workarounds for that, let me know! Otherwise I can hopefully figure out treating them as a very thin area myself.

    Thanks for any assistance anyone can provide...
     
    Last edited: Apr 12, 2024
  50. AndyBlock

    AndyBlock

    Joined:
    Oct 12, 2016
    Posts:
    35
    I dug a bit deeper on this - I was thinking to try to render the mesh myself in the RenderGizmoSelected - then it occurred to me to check out how it works for built in NavMeshSurface. I discovered the AI Navigation package's NavMeshSurfaceEditor has some code for "UNITY_2022_2_OR_NEWER" which does this:

    navSurface.navMeshDataInstance.FlagAsInSelectionHierarchy();


    I tried adding that to NavMeshPlus's NavMeshSurface, but unfortunately that FlagAsInSelectionHierarchy method is internal and therefore not accessible. However, calling it via reflection does seem to do the trick:


    [DrawGizmo(GizmoType.Selected | GizmoType.Active | GizmoType.Pickable)]
    static void RenderBoxGizmoSelected(NavigationSurface navSurface, GizmoType gizmoType)
    {
    var method = navSurface.navMeshDataInstance.GetType().GetMethod("FlagAsInSelectionHierarchy", BindingFlags.NonPublic | BindingFlags.Instance);
    method.Invoke(navSurface.navMeshDataInstance, new object[] { });

    RenderBoxGizmo(navSurface, gizmoType, true);
    }


    It's hacky, for sure, but at least I can see my navmesh now and debug other issues. Hope this is of use to someone, and that the problem can be resolved somehow in NavMeshPlus in the future (hopefully without hacky reflection calls!).