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

Can you overlap two nav mesh surfaces (so agent uses both)?

Discussion in 'Navigation' started by RamSteelwood, Jul 27, 2018.

  1. RamSteelwood

    RamSteelwood

    Joined:
    Mar 6, 2013
    Posts:
    19
    This might make more sense with a bit of background.

    Working on adding AI cars to my driving game, where I have road gameobjects on a terrain. I want the nav agent to use the roads primarily, but still be able to navigate 'offroad' when needed.

    If I bake a single nav mesh surface (using navmesh components), setting the general area to be 'offroad', and using a modifier on the road gameobjects to define a road area - I get a nice neat navmesh, and the agent will navigate along the road primarily, based on area costs.
    The problem though is that this bakes the edge of the road area right on the edge of the road objects, and the agent then targets the edge, which would result in the car generally being half off road.

    If I bake a separate nav mesh surface just for the road objects, it creates a 'border' to the edge of the road based on the navmesh agent radius...this keeps the agent navigating more within the road, but it then can't/won't go offroad.

    So what I tried to do was bake two surfaces - one for the terrain as 'offroad', including the terrain 'under' the roads, with an area with a high cost.
    I then baked a second surface for just the road objects, with a low cost, which had the narrower 'driveable' section of the road.
    The hope was that the agent would nav across both surfaces, prioritising the lower cost area on the 'road' navmesh, but going offroad when needed.
    The two surfaces together looked nice in the editor, but in practice the agent just seems to ignore the road surface and solely navigates using the 'offroad' surface. I tried changing the 'order' the areas were set in the nav mesh areas, in case it was based on the index, but it didn't seem to make a difference.

    So if that makes any sense....
    Is it possible to run two surface areas together like this, and if so how?
    And/or, is there a simple way I can do this with a single surface, but tell my agent to navigate away from the edge of the road area?