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

What Does RayCast Do For a NavMesh?

Discussion in 'Navigation' started by VengeanceDesign, Jun 30, 2016.

  1. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Hi everyone,

    I've been experimenting with Unity's navmesh system and I'm having trouble understanding what the Navmesh raycast and samplepoint function returns. How does the result of a navmesh raycast or samplepoint function call relate to the actual terrain? If I have baked the navmesh for the interior of a building and I raycast downwards from a point above the floor, should the resulting point be located somewhere on the floor of the navigable part of the room? The documentation says that raycasts for Navmeshes are done in 2.5D but will my resulting intersection point be the actual point in world space that the raycast hit?

    - Thanks in advance for anyone willing to clear this up.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    NavMesh.Raycast is poorly named. Think of it as starting at a point at the navmesh, and then drawing a line along it. It will return true if that line hits the edge of the navmesh, and the hit data will be where that edge is.

    So you can check things like "Is there a direct path between me and some target along the navmesh?". Not quite sure what it should be called (TracePath?), but this is the second thread in two weeks where somebody has read the docs and doesn't understand what it does, so they should probably do something about it.

    SamplePoint doesn't exist. SamplePosition, which you probably meant, is pretty simple. You give it a point in world space, and it gives you the point on the navmesh closest to that point. The radius is how far out the check should go.
     
    RobSCoatsink, edwiz7 and eses like this.
  3. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Thank you for explaining that Baste. So are you saying that navmesh raycasts will always follow the plane of the navmesh triangles rather than the actual direction between start and destination? What happens when I raycast directly up? Can I detect ceilings or floors?
     
  4. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Thank you for explaining that Baste. Are you saying that Navmesh Raycasts always follow the plane of the navmesh polygons, so I can't just raycast up/down and find a floor or ceiling?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Pretty much, yes. Note that the second argument of Navmesh.Raycast is destination, not direction.

    I have no idea what happens of the origin or destination of Navmesh.Raycast is too far outside of the navmesh. The docs state that "The source and destination points are first mapped on the NavMesh", but how that is done is not mentioned. I imagine that SamplePoint is used internally, but there's no guarantees that that's the case. If you need to know, I suggest setting up a test.
     
  6. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Well come to think of it I probably won't need to find navigation mesh raycasts to find floors or ceilings, though I do use them to find random positions near something they should investigate, which may be position above or below them. In any case, thanks. It's good to know that I can use navigation mesh raycasts for things like finding strafing paths (without using a proper raycast).