Search Unity

ARFoundation and Navigation

Discussion in 'Navigation' started by ar2056, Oct 26, 2018.

  1. ar2056

    ar2056

    Joined:
    Oct 17, 2018
    Posts:
    8
    Hello,

    I am trying to figure out how to implement navigation on ARPlanes, that keeps changing size as more feature points are getting detected.

    I have this code for dealing with variable size ARPlanes:

    Code (CSharp):
    1. void boundaryChangedFunction(ARPlaneBoundaryChangedEventArgs aRPlaneBoundaryChangedEventArgs)
    2.     {
    3.         ARPlane tempPlane = aRPlaneBoundaryChangedEventArgs.plane;
    4.         var setPlane = m_ARPlaneManager.planePrefab;
    5.        
    6.         NavMeshSurface tempNavMeshSurface = tempPlane.gameObject.GetComponent<NavMeshSurface>();
    7.        
    8.         if (tempNavMeshSurface)
    9.         {
    10.             Debug.Log(tempPlane.name + " has a NavMeshSurface, will bake it!");
    11.            
    12.             tempNavMeshSurface.BuildNavMesh();
    13.         }
    14.         else
    15.         {
    16.             Debug.Log(tempPlane.name + " does not have a NavMeshSurface. Will add one and bake!");
    17.             tempPlane.gameObject.AddComponent<NavMeshSurface>();
    18.            
    19.             tempPlane.gameObject.GetComponent<NavMeshSurface>().BuildNavMesh();
    20.         }
    21.  
    22.     }
    You can see that each time the plane changes size then I am calling "BuildNavMesh();". No performance issues so far.

    The problem arises when I try to add an agent. I add a small AR object by tapping on the plane and in my Update() method I have:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.touchCount > 0)
    4.         {
    5.             Touch touch = Input.GetTouch(0);
    6.  
    7.             if (m_SessionOrigin.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon))
    8.             {
    9.                 Pose hitPose = s_Hits[0].pose;
    10.                
    11.                
    12.  
    13.                 if (spawnedObject == null)
    14.                 {
    15.                     spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation);
    16.                  
    17.                     navAgent = spawnedObject.AddComponent<NavMeshAgent>(); //THIS CAUSES THE PROBLEM
    18.  
    19.                 }
    20.             }
    21.         }
    22.  
    23.     }
    But I keep getting this error: "Failed to create agent because it is not close enough to the Navmesh".

    Then when I try to move the agent nothing happens. The issue with AR is that since all objects are dynamically added I can't use the drag & drop feature of Unity, as it is usually done for static scenes.
     
    EWimsett likes this.