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:
    353
    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
  2. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    353
    Bump.
     
  3. mohydineName

    mohydineName

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

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    353
    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:
    353
    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.
     
  6. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    353
    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!!!
     
  9. castor-09

    castor-09

    Joined:
    Mar 2, 2014
    Posts:
    2

    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. }
     
  12. Helical

    Helical

    Joined:
    Mar 2, 2014
    Posts:
    44
    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
     
  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. JamesAMD

    JamesAMD

    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 floreNz 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.
     
  18. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    554
    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,110
    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
    avsaninwork8 and astracat111 like this.
  20. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    554
    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:
    765
    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:
    308
    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:
    53
    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:
    2
    This solved it for me, too. Thanks so much!! This was bugging me for months...
     
  26. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    765
    I am glad my tip was helpful for some of you!
     
  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:
    1
    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
     
    Nicolas_Magno and astracat111 like this.
  29. Nicolas_Magno

    Nicolas_Magno

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

    llamirande

    Joined:
    May 14, 2019
    Posts:
    1
    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
     
unityunity