Search Unity

NavMesh.SamplePosition handles NavMesh Obstacles?

Discussion in 'Navigation' started by Multithreaded_Games, Oct 21, 2016.

  1. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    Hi all,

    Short and sweet question: if I call SamplePosition on a position that is currently within the 'carve' of a NavMeshObstacle, will SamplePosition correctly return the nearest navigable position not in the carve radius, but on the actual navmesh? Or does it not handle obstacles at all and just go with whatever the original baked navmesh said?

    Going to say tentatively that the latter is the case, but it's a bit hard to test currently--just wondering if anyone knew definitively. Thanks!
     
    CasualT_Bossfight likes this.
  2. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Just tested it. Obstacles don't seem to influence the result of SamplePosition, regardles of 'carve' or not (which is both sad and unsuspected)
     
  3. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    Thanks a lot for taking the time to try it out. It really is a bummer. I want to love Unity, but it gives me so many reasons to distrust it, haha. Here's to hoping they improve the entire navigation system soon!
     
  4. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Yeah. I planned on using SamplePosition in conjunction with obstacles, but apparently I have to scratch that thought. At least the agent still doesn't walk right into the obstacle.
     
    Multithreaded_Games likes this.
  5. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    You should still be able to do it with an extra step:

    What we're doing is 'generating' a bunch of potential movement positions (turn-based tactical RPG, but without a square or hexagonal grid) around the acting character and testing/adjusting the generated position based on NavMesh.SamplePosition. When a character is acting, all other characters have their NavMeshObstacles enabled and their NavMeshAgents turned off. It's funny to me that the acting character is able to avoid the obstacles, but SamplePosition doesn't actually work right lol. Wtf. That being said, we just take the position and do a capsule collision check to see if there is anything actually there to prevent it from moving to a 'sampled' navmesh position that is actually inside a carved area. It works well enough, but it would be better to cut out the collision check entirely.

    Now if only Unity would add support for NavMesh agents with varying sizes... we have to essentially have all of our enemies and characters have the same NavMesh radius for this to work properly. Not ideal at all, haha. You'd think that would be a pretty obvious feature, but alas...

    Anyhow, good luck with your project!
     
  6. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Thank you. It seems like we are working on quite similar projects (Although I am not yet entirely sure if I want to use Unity's navigation system or implement one myself with a grid. I am still thinking about how to implement ladders, or climbing in general. Need to look more into off-mesh-links).
     
  7. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    NavMeshObstacle carving happens with a delay of one frame - SamplePosition should work as expected if you account for that frame delay.
     
    Multithreaded_Games and Piflik like this.
  8. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Thank you for your answer. I ran my quick tests in Start(), so the mesh was probably not updated yet, and with a one frame delay it did actually work.
     
  9. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    Thanks for the clarification, Jakob!
     
  10. NewMagic-Studio

    NewMagic-Studio

    Joined:
    Feb 25, 2015
    Posts:
    454
    Actually keeps giving a position inside the carve at any frame after creating the carve. I am instantiating a cube which destroys in 3 seconds and the cube keeps appearing in a area with a model which created a carve and didnt move again
     
    JBR-games likes this.
  11. MartyMcFly

    MartyMcFly

    Joined:
    May 2, 2013
    Posts:
    16
    As this thread confused the hell out of me: Yes, it works. The NavMeshObstacle is honored. (Unity 2019.4.4)

    - Make sure SamplePosition() returns true
    - Don't use SamplePosition() around an object you placed during the same frame - wait for one frame as mentioned above.
     
  12. ilyablokh

    ilyablokh

    Joined:
    Mar 21, 2020
    Posts:
    2
    I have same problem
    - SamplePosition() returns true
    - Obstacles are spawned on the scene earlier than I call SamplePosition()
    And SamplePosition hit.position is still inside Obstacle

    --------UPDATE---------
    I handle it
    I toggled Carving on obstacle and use NavMesh.Raycast instead on SamplePosition
     
    Last edited: Dec 29, 2021