Search Unity

"Failed to create agent because it is not close enough to the NavMesh"

Discussion in 'Editor & General Support' started by PeterB, Feb 29, 2012.

  1. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    I have a large terrain with a navmesh, running on an authoritative server running Unity 3.5 and uLink. Everything works very nicely, but when creating a NavMeshAgent at runtime, Unity comes back with the following error message:

    Code (csharp):
    1. Failed to create agent because it is not close enough to the NavMesh
    However, as you can see from the attachment, the agent is right on the navmesh.

    Googling the error message yields absolutely nothing useful, apart from an irrelevant forum posting on these forums (sans reply). There are no other NavMeshAgents active, pathfinding works fine in other scenes, no performance problems. The terrain is 4000x4000. The navmesh isn't a heightmesh. Should it be? The Y value of the NavMeshAgent is just over 300. I can't but wonder if the navmesh is treated as conceptually flat, with a Y value of 0.

    Filed as a bug. I'll post the case # as soon as Unity's automated bug tracking system comes back with one.

    Anyway, I'm completely stumped. There's hardly any documentation, the error message is clearly wrong unless the graphics are misleading and a heightmesh is required.

    Any ideas, anyone?
     

    Attached Files:

    Last edited: Feb 29, 2012
    Tymianek likes this.
  2. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    Bump.
     
    Tymianek likes this.
  3. mohydineName

    mohydineName

    Joined:
    Aug 30, 2009
    Posts:
    301
    Same issue here
     
    Tymianek likes this.
  4. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    Trying to build a heightmesh yields the error, "buildNavigation: Could not build compact data.". Judging from the Recast source, this doesn't seem to be an out of memory error.
     
  5. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    Bump. Doesn't this issue merit at least some response from someone in the know? I'd be more than happy to provide a minimal set.This is a blocker for my project.
     
    AldeRoberge likes this.
  6. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    I took a closer look at this. It *is* memory-related, after all; however, Unity provides misleading information *and* behaves in a way which hides this fact. The terrain of 4000x4000 is obviously very large (and really would require a hierarchical approach). Nevertheless, if the bake completes as expected, then the results should be as expected. However, they're not:

    1. When baking the navmesh on my 4 GB RAM machine, everything *appears* ok - no error messages are given, everything just completes as expected. However, the resulting navmeshes are unusable.

    2. I then added another 4 GB RAM to the machine, for a total of 8 GB RAM. I then kept a close watch on free memory during the bake. As Recast slowly required more and more memory, I had to quit the other two, three applications running (Chrome, Mail). When the bake was ready to be imported, Unity crashed hard. Repeating the experiment after restarting the computer resulted in the following error message, produced just before the mesh was to be imported:

    Thus another 0.5 GB was required but not available. The amount of free RAM was very slightly lower than this.

    So in this case, when *more* memory was added, the bake crashed with an out of memory error message, whereas in the first case, where only half of the amount of memory was available, everything looked just the way it should. This is clearly very misleading behaviour from Unity.

    I'm now going to try the same bake on a machine with 16 GB RAM. Watch this channel for more news. (The case # is 451260)
     
    Last edited: Mar 17, 2012
    malkere likes this.
  7. slightchan

    slightchan

    Joined:
    Nov 2, 2012
    Posts:
    1
    Hey guys, I met the same problem, after reviewing the script reference, I found the right way to add NavMeshAgent at runtime, NavMesh.SamplePosition is the key, here's the sample code:

    Code (csharp):
    1. GameObject go = new GameObject("Target");
    2. Vector3 sourcePostion = new Vector3( 100, 20, 100 );//The position you want to place your agent
    3. NavMeshHit closestHit;
    4. if( NavMesh.SamplePosition(  sourcePostion, out closestHit, 500, 1 ) ){
    5.   go.transform.position = closestHit.position;
    6.   go.AddComponent<NavMeshAgent>();
    7.   //TODO
    8. }
    9. else{
    10.   Debug.Log("...");
    11. }
    http://docs.unity3d.com/Documentation/ScriptReference/NavMesh.SamplePosition.html

    Hope this could help :D
     
  8. USPowerSquadron

    USPowerSquadron

    Joined:
    Jan 10, 2014
    Posts:
    1
    I just ran across this thread when I was getting this issue. slightchan's solution fixed it. Thank You!!!
     
    vm_unity336 likes this.
  9. castor-09

    castor-09

    Joined:
    Mar 2, 2014
    Posts:
    3

    It was best answer for my problem. I want to create Navimeshagent object.
     
  10. nicoguerra1337

    nicoguerra1337

    Joined:
    Nov 12, 2014
    Posts:
    3
    I have a somewhat similar problem. I get the same error message, but it's when I'm spawning multiple enemies. Then the NavMeshAgent doesn't make the enemies follow the player. Later, I forgot what I did, but I don't get that message anymore. Even still, the enemy won't follow the player.

    HELP PLEASE!
     
  11. Zyxil

    Zyxil

    Joined:
    Nov 23, 2009
    Posts:
    111
    Simple hack to force GO onto the NavMesh. Place on same GO as the NavMeshAgent.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// <summary>
    5. /// Used to make sure the GO is on the NavMesh.
    6. /// </summary>
    7. public class NavMeshInstantiator : MonoBehaviour
    8. {
    9.     void Start()
    10.     {
    11.         NavMeshHit closestHit;
    12.  
    13.         if (NavMesh.SamplePosition(gameObject.transform.position, out closestHit, 500f, NavMesh.AllAreas))
    14.             gameObject.transform.position = closestHit.position;
    15.         else
    16.             Debug.LogError("Could not find position on NavMesh!");
    17.     }
    18. }
     
    KemonoKevin and TJeyAGames like this.
  12. Helical

    Helical

    Joined:
    Mar 2, 2014
    Posts:
    50
    Encountered the problem, Tried deleting the nav mesh object (in my case a cylinder) and then placing it again in the scene did the trick.

    My theory is that some kind of Unity "Run In Editor" script which attaches agents to nav meshes is screwed up. and doesn't always work, and is run each time you place a new NavMeshAgent component in the scene hierarchy
     
    KemonoKevin and IcyFrosty12 like this.
  13. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    If anyone is still having this problem it was because I was spawning my enemies to high before they could link with the navmesh. I lowered my spawns so that when they spawn the agents collider in the ground.
     
    DDeathlonger likes this.
  14. James_0124

    James_0124

    Joined:
    Feb 21, 2015
    Posts:
    5
    I encountered the same problem. Slightchan's solution worked.
     
  15. rindoh

    rindoh

    Joined:
    May 20, 2013
    Posts:
    1
    recovery method:
    Code (CSharp):
    1. agent = GetComponent<NavMeshAgent> ();
    2. if(!agent.isOnNavMesh) {
    3.     transform.position = somewhereOnmeshPosition;
    4.     agent.enabled = false;
    5.     agent.enabled = true;
    6. }
     
  16. Nickname219

    Nickname219

    Joined:
    Mar 16, 2014
    Posts:
    2
    I fixed this bug just translate terrain into zero position
     

    Attached Files:

    hfro and Torte89 like this.
  17. QuintenOne

    QuintenOne

    Joined:
    Sep 3, 2016
    Posts:
    1
    This works if the agent is activated before the mesh itself is generated.
    Code (csharp):
    1. if(Time.time > .1f) {
    2.     GetComponent<NavMeshAgent>().enabled = true; }
    You can use any value to check, but .1f is probably enough to generate your mesh and not enough to get annoying.
     
    neargames105 likes this.
  18. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Remember also that you have to put at the top of your scripts guys:

    Code (CSharp):
    1. using UnityEngine.AI;
     
  19. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    I'm generating NavMeshData in runtime. Was working in 200x200 test scene. Not working in multiple 1024x1024 tiles. Even with the sample height trick, though I'm enabling the agent 2 lines before the reposition... Will try fixing call order and running smaller sections.

    edit: in my case it turned out to be an agentTypeID problem. In specifically setting the agent.agentTypeID to the same ID used in the NavMeshBuildSettings everything is working fine now. The agents don't even have to be that close to the navmesh.
     
    Last edited: Mar 1, 2018
  20. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    This may have been my problem as well. Right now I'm using the workaround, which is to turn the navmeshagent on and off, then warp it to its own position.
     
  21. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    I got that error while using the new navMesh workflow and the problem was that the navMeshAgent in the character had a different radius than the one in the NavMeshSurface. In my case the navMeshAgent was 0.2f radius and the navMeshSurface had 0.5f radius.
     
    zeinokko and DigiT0L like this.
  22. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I used a combo of Zyxil and rindoh's methods to get it to work on a prefab with the navmesh agent already on it. I just keep the navmesh agent disabled on the prefab and enable it after placing it on the navmesh.
    Code (CSharp):
    1. NavMeshAgent agent = GetComponent ("NavMeshAgent") as NavMeshAgent;
    2. NavMeshHit closestHit;
    3. if (NavMesh.SamplePosition (transform.position, out closestHit, 100f, NavMesh.AllAreas)) {
    4.     transform.position = closestHit.position;
    5.     agent.enabled = true;
    6. }
     
  23. DigiT0L

    DigiT0L

    Joined:
    Dec 16, 2015
    Posts:
    1
    This fixed my problem. Matching the agent with the Baked Map FYI
     
  24. b1gry4n

    b1gry4n

    Joined:
    Sep 11, 2013
    Posts:
    146
    i had this problem. I was instantiating a prefab with a navmesh agent on a child game object. instead i instantiated the navmesh agent at runtime as its own prefab and it seems to be working now
     
  25. zeinokko

    zeinokko

    Joined:
    Aug 5, 2016
    Posts:
    4
    This solved it for me, too. Thanks so much!! This was bugging me for months...
     
  26. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    I am glad my tip was helpful for some of you!
     
    kamal87635 likes this.
  27. daniel_kovacs

    daniel_kovacs

    Joined:
    Jan 8, 2019
    Posts:
    1
    Very helpful -- thank you very much!
     
  28. Waius

    Waius

    Joined:
    Mar 17, 2019
    Posts:
    2
    the same thing happened to me but I solved it by deleting the NavMeshAgent of each of the prefabs that owned it and putting them back on, I hope this helps someone
     
  29. Nicolas_Magno

    Nicolas_Magno

    Joined:
    May 12, 2015
    Posts:
    3
    It worked!!!
     
  30. llamirande

    llamirande

    Joined:
    May 14, 2019
    Posts:
    2
    The main issue ive had was having the NavMeshAgent colliding with the terrain, somehow causes the issue of saying its too far away. I usually raise it up 0.001 on the y value and it works. Not sure why
     
  31. Supershura

    Supershura

    Joined:
    May 13, 2019
    Posts:
    1
    Don't forget to check Agent Type, because I had "New Agent" in my Agent and changing it to Humanoid helped me.
     
    AftimusPrime likes this.
  32. Michelangelo_

    Michelangelo_

    Joined:
    Mar 4, 2020
    Posts:
    1
    At first I made 4 different Agent type for each of the players in my game.
    I assigned the same Agent type to the different players and it has worked. It seems not working with more than one agent type...or I don't know how to use more than one XD
     
    nailmail likes this.
  33. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    You would need to render a new navmesh per agent type. Not recommended. That's for when you have for example a big monster that can't fit through the same doors or something that needs a different navmesh than others.
     
    nailmail and Michelangelo_ like this.
  34. Deleted User

    Deleted User

    Guest

    I ran into this today too, thanks to this thread for helping me find a solution. I already had my agent on the navmesh (using a SamplePoint call) but it still wasn't working, but a simple "turn it off and on again" worked:

    Code (csharp):
    1. var agent = Instantiate(prefab, spawnPosition).GetComponent<NavMeshAgent>();
    2. agent.enabled = false;
    3. agent.enabled = true;
    This is really dumb, but something musn't be refreshing in the AI upon instantiation.
     
  35. ahmedsaed2652003

    ahmedsaed2652003

    Joined:
    Mar 25, 2020
    Posts:
    5
    the problem happens when you put a collider on the agents

    when spawning alot of them they push each other (due to colliders) outside the navmesh which creates the error
    in order to fix this you need to make sure that they are spawning away from each other

    this is my code :
    Code (CSharp):
    1. for (int i = 0; i < gameObject.transform.childCount; i++)
    2.             {
    3.                 if (Vector3.Distance(finalPosition, gameObject.transform.GetChild(i).transform.position) > 0.5)
    4.                     //SpawnCode
    5.             }
    i this code i am looping over each agent (child of Gameobject) then i am checking if the distance between spawn position (finalPosition) and agent position is grater than 0.5
     
  36. CasualDutchman

    CasualDutchman

    Joined:
    Jan 20, 2016
    Posts:
    37
    I stumbled upon this thread after I had some problems with the agent.
    In my case, I instantiate the agent and in the lines of code after it, I changed the position and rotation.
    Code (CSharp):
    1. var enemy = Instantiate(enemyprefab);
    2. enemy.transform.position = spawnLocation.position;
    3. enemy.transform.rotation = spawnLocation.rotation;
    This however messes with how the navmeshagent is placed.
    I guess it is placed on 0, 0, 0 at the first frame and is placed to the specified position later.

    I found that setting the position within the instatiate method to work.
    Code (CSharp):
    1. var enemy = Instantiate(enemyprefab, spawnLocation.position, spawnLocation.rotation);
    If you want to set a parent, you can do so a line below.

    This worked for me. Hopefully it helped some of you as well!
     
  37. seseguy

    seseguy

    Joined:
    Nov 30, 2019
    Posts:
    10
    Hmm, looks strange to me, because I always spawn my navmeshagents with Instantiate(prefab, position, rotation) and this is where i get the error. You say that setting pos and rot inside instance works for you, but this is where i get that error. Heh.
     
  38. contact_unity364

    contact_unity364

    Joined:
    Sep 28, 2020
    Posts:
    5
    I had this issue, and I had my agent dimensions set consistent with the bake dimensions, but I had created a second agent type called "animal" initially with wider dimensions to humanoid, but then I made them the same, it made no difference I could not get rid of this error message so just removed the second agent type. Solved the immediate issue but have no idea why that did not just work.
     
  39. mateuszwallace

    mateuszwallace

    Joined:
    Dec 13, 2019
    Posts:
    17
    Hello, I had very similar issue. It happens when I tried update navMesh in runtime.
    At first few second everything was correct however after while had some crazy behaviour becuase after updating navMesh my agent freezes.
    After few days I noticed that one instantiatet object is moving far away (falling) and when hes position was "huge" its block updating navMeshSurf (I using Physics Collider for geometry). I removed this falling GameObject and everything was back to casual behaviour. So look into your scene and check if others object didnt block this functions probably because of some crazy numerical values during navMesh generation.
     
  40. Oskadddd

    Oskadddd

    Joined:
    Oct 26, 2020
    Posts:
    1
    just found a solution, all you need to do is move your agent down so it touches the Nav Mesh
     
  41. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    there are a number of different possible causes resulting in the same error
     
  42. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    My issue happend than GameObject with NavimeshAgent was instantiated.
    Keeping NavimeshAgent component disabled solved the issue.
    (it will be enabled than I need this).
     
  43. Michael_Berna

    Michael_Berna

    Joined:
    Jul 5, 2019
    Posts:
    39
    This worked great, thank you so much for sharing slightchan! I spent forever trying to figure this error out with a spawning and AI asset I've been trying to use.

     
  44. neargames105

    neargames105

    Joined:
    Aug 14, 2020
    Posts:
    1
    Thanks , you’ve saved my life!
     
  45. Djelatin

    Djelatin

    Joined:
    Aug 17, 2022
    Posts:
    1
    For whoever may be wondering I have experienced this problem on my player. He just wouldn't move. Tried everything and then I figured it out. When In removed collider off of my parent obj I was left with a small capsule type of collider. It was on top of my mesh so I made my mesh invisible and moved NavMeshAgent collider thingy all the way down to terrain and It fixed it for me. I am still very new to all this, so if anyone has the same problem as me, there is your solution.
     
  46. hworld

    hworld

    Joined:
    Mar 18, 2018
    Posts:
    6
    In my case, it's the field `NavMeshAgent.areaMask` .

    I change it's value in the code. But I have misunderstood the api.

    Code (CSharp):
    1. // new
    2. navMeshAgent.areaMask = 1 << NavMesh.GetAreaFromName("Walkable");  
    3.  
    4. // old   this is wrong
    5. navMeshAgent.areaMask = NavMesh.GetAreaFromName("Walkable");    
    And another point is agent type.
    It must be `Humanoid`, otherwise, it does not work.
    More info maybe found in: https://answers.unity.com/questions/1358023/nav-mesh-agent-type.html
     
    Last edited: Feb 10, 2023
  47. tukyo

    tukyo

    Joined:
    Feb 27, 2023
    Posts:
    6
  48. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    Yep, if you bake a NavMesh for agent type 'EnemyRobot' and try to place a humanoid agent on it, it will cause this error. You need to make sure the agent type and the NavMesh type match.
     
  49. harsuugame

    harsuugame

    Joined:
    Mar 11, 2023
    Posts:
    3
    Cannot add menu item 'GameObject/AI/NavMesh Surface' for method 'UnityEditor.AI.NavMeshSurfaceEditor.CreateNavMeshSurface' because a menu item with the same name already exists.

    not solve help me ??
     
  50. TomGrzembke

    TomGrzembke

    Joined:
    Mar 28, 2022
    Posts:
    1
    The Agent type of my objects reset somehow and my navmesh wasn't baked for humanoid, simple rebeaking fixed it.