Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question SetDestination() isn't setting the destination in one scene, is in another. No idea why

Discussion in 'Navigation' started by infinitypbr, May 8, 2020.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Hi all,

    I have some pretty simple NavMeshAgent code:

    Code (CSharp):
    1. public void SetDestination()
    2.     {
    3.         if (navMeshAgent.isOnNavMesh)
    4.         {
    5.             Debug.Log("Setting Destination to " + desiredDestination);
    6.             navMeshAgent.SetDestination(desiredDestination);
    7.  
    8.             Debug.Log("NavMeshDestination is " + navMeshAgent.destination);
    9.         }
    10.     }
    In a test scene with a flat terrain, it works fine.
    Debug.Log
    shows the updated destination, the actor moves, all that.

    In my main scene (which is the damascene from the Meadows package from NatureManufacture), the code doesn't work. There are no errors, no warnings. The
    Debug.Log
    simply shows the same
    navMeshAgent.destination
    regardless of how many times I attempt to
    SetDestination()
    .

    I'm at a complete loss on how to trouble shoot this. I've rebaked the NavMesh multiple times. Moved the agent around to flat area. Nothing seems to work, and since there are no errors, I have no idea where to look for a fix.

    Any thoughts? Thanks!
     
  2. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Someone on the Messy Coders discord has helped me out:

    Code (CSharp):
    1. private bool SetDestination(Vector3 targetDestination)
    2. {
    3.   NavMeshHit hit;
    4.   if (NavMesh.SamplePosition(targetDestination, out hit, 1f, NavMesh.AllAreas))
    5.   {
    6.     agent.SetDestination(hit.position);
    7.     return true;
    8.   }
    9.   return false;
    10. }
    The problem I was facing, the random position I was choosing was much higher (on the y axis) than the navmesh, so it was failing.
     
    tchris likes this.