Search Unity

Changing Max Jump Distance in Unity Nav Mesh

Discussion in 'Navigation' started by aFeesh, Dec 5, 2019.

  1. aFeesh

    aFeesh

    Joined:
    Feb 12, 2015
    Posts:
    24
    I'm using NavMeshSurfaces, and NavMeshObstacles to carve my surface. I have a NavMeshAgent that can place NavMeshObstacles on top of itself.

    Is there a way to change the max distance my NavMeshAgent will jump when a NavMeshObstacle is carved on top of its location? I've attached a GIF illustrating Unity's current behavior, as you can see, it lets you jump about 6 units, and if there's no NavMesh in a radius of 6 units around the NavMeshAgent it doesn't move the NavMeshAgent anywhere. If there is no setting then this must be a bug, I would expect the NavMeshAgent to either never jump, or to always jump if a NavMesh is available.

    I thought the setting was https://github.com/Unity-Technologi...I/Public/NavMeshBuildSettings.bindings.cs#L34 but as it turned out this was just 0, and unrelated.
     

    Attached Files:

  2. wildframemedia

    wildframemedia

    Joined:
    Feb 13, 2013
    Posts:
    24
    When an Agent is enabled it'll try sticking to being on a NavMesh. When not on a compatible surface it'll probably do something similar to a SamplePosition and a Warp to that position. As far as I know there is no way of tweaking this value, and probably for a good reason, as doing a SamplePosition with too big of a radius is really inefficient.

    You can handle this yourself and that way you'll be able to consistently move the agent somewhere. However I'd recommend performing several smaller SamplePositions (at pseudo-random locations) in an increasing range instead of a single Sample with a huge radius.
     
  3. aFeesh

    aFeesh

    Joined:
    Feb 12, 2015
    Posts:
    24
    @wildframemedia Wow thanks! SamplePosition is exactly what I need I think. So you're saying I can detect if my NavAgent is no longer on a NavMesh (due to carving), and if so I can call SamplePosition to find a new closest point (and optimizing by using a small max distance). Is there a method to detect when my NavAgent is no longer on a NavMesh? Or even better, is there a way to intercept when a NavMeshObstacle is carved on top of a NavAgent? So that I can call SamplePosition at the correct time.
     
  4. wildframemedia

    wildframemedia

    Joined:
    Feb 13, 2013
    Posts:
    24
    You can check if the agent is on a NavMesh by checking the property isOnNavMesh ,however, since the agent automatically moves to a close enough surface this might not work for you. If the automatic teleport works for you and you simply want to make it more reliable I'm guessing you could check this to do the teleport yourself when you are out of the mesh.
    Otherwise I think you'll need to detect the situations when your agent might carve the ground beneath its feet yourself.
     
    aFeesh likes this.
  5. aFeesh

    aFeesh

    Joined:
    Feb 12, 2015
    Posts:
    24
    @wildframemedia Thanks for the insight, you are right I can detect when that sort of carving is happening, and do my own SamplePosition. I'd still have to wait for the carving to occur, in order for the SamplePosition to not just grab the newly carved position, which means the player will jump using the default logic first before my logic gets a chance to apply. Probably an execution order thing that I can look into myself.

    However, reading the SamplePosition documentation I came across this: "For example, you the sourcePosition is on the ceiling, a point on the second floor will be returned (if there is NavMesh there), instead of floor position on the first floor." I definitely do not want this sort of behavior, so I spun up a new thread here if you have any thoughts on how to fix this.