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

Hi all,

I have some pretty simple NavMeshAgent code:

public void SetDestination()
    {
        if (navMeshAgent.isOnNavMesh)
        {
            Debug.Log("Setting Destination to " + desiredDestination);
            navMeshAgent.SetDestination(desiredDestination);

            Debug.Log("NavMeshDestination is " + navMeshAgent.destination);
        }
    }

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!

Someone on the Messy Coders discord has helped me out:

private bool SetDestination(Vector3 targetDestination)
{
  NavMeshHit hit;
  if (NavMesh.SamplePosition(targetDestination, out hit, 1f, NavMesh.AllAreas))
  {
    agent.SetDestination(hit.position);
    return true;
  }
  return false;
}

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.

1 Like