Search Unity

SetDestination generating a path further away than necessary

Discussion in 'Navigation' started by malkere, Mar 2, 2018.

  1. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    navmesh1.jpg
    If I stand on the navmesh they all run up the ramp to me just fine. If I stand a milimeter off of it they all run beneath, a y distance of roughly 3.2m lower.

    I'm using the code:
    Code (CSharp):
    1.         NavMeshHit hit;
    2.         if (NavMesh.SamplePosition(target.position, out hit, 15f, NavMesh.AllAreas)) {
    3.             destination = hit.position;
    4.             agent.SetDestination(destination);
    5.         }
    where target is the player transform, because simply setting the destination was causing this behavior, but SamplePosition did not remedy the problem.

    Why would the agent generate a path to a point further away than is possible? Maybe x and z planes are given priority over y?

    I will try to generate spheres using SamplePosition to see what's up and try smaller ranges when I get home. Any ideas?
     
  2. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    creating a sphere does indeed place it beneath me despite standing right next to a much closer mesh. I tested going another layer up without updating the NavMesh and sure enough as long as I am x, z over a layer it will read the closest y fit, but if there is a closer x, z fit regardless of my y it will prioritize that.... which is of course ridiculous.

    In the meantime I suppose I check can five directions near the player and see if any of the SamplePositions return a value that is actually close to the player and use that, but that's several times the computing power =[

    navmesh2.jpg

    I'm running 2017.1 btw
     
    Last edited: Mar 2, 2018
  3. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    Currently I'm checking in four directions, to find a sample near the player in 3D not just x, z:
    Code (CSharp):
    1.         NavMeshHit hit;
    2.         NavMesh.SamplePosition(target.position, out hit, 1f, NavMesh.AllAreas);
    3.         if (Vector3.Distance(hit.position, target.position) < 0.1f) {
    4.             destination = hit.position;
    5.             agent.SetDestination(destination);
    6.             return;
    7.         }
    8.         NavMesh.SamplePosition(target.position + new Vector3(1f, 0, 1f), out hit, 5f, NavMesh.AllAreas);
    9.         if (Vector3.Distance(hit.position, target.position) < 1.5f) {
    10.             destination = hit.position;
    11.             agent.SetDestination(destination);
    12.             return;
    13.         }
    14.         ...
    And it's working decent. I can still go to the extreme edge of a building and things can't get to me, but good enough for now.