Search Unity

Question any way for free-roaming NPC to prefer "roads"?

Discussion in 'Navigation' started by VayneVerso, Jan 12, 2023.

  1. VayneVerso

    VayneVerso

    Joined:
    Dec 5, 2022
    Posts:
    18
    I might be missing something extremely easy. If I've got a hilly terrain, say (see the attached screenshot), with a more level path through part of it (that a human player would clearly understand is the intended pathway), I'd probably want an NPC to also prefer using the "road", so to speak. Is know that I can set-up different areas with different navigation costs in the the Navigation panel, but is there some way to, say, paint the area that I want to be considered the best path so that's factored into the bake?
     

    Attached Files:

    • road.jpg
      road.jpg
      File size:
      40.3 KB
      Views:
      106
  2. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    If I'm correct navmesh cannot use painted areas, only colliders and meshes.
     
  3. VayneVerso

    VayneVerso

    Joined:
    Dec 5, 2022
    Posts:
    18
    So I'm guessing if you want to simulate NPCs preferring roads, you have to painstakingly create waypoints along routes you think they should take and have them periodically do visual checks to move between them. I can probably do that, though it would be cool if this kind of functionality were built into the terrain building features.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Random name user, you could probably look int Nav Mesh documentation where this is
    explained. See the topic of navigation areas and costs.
    You can basically set the A* value of different regions of your traversable surfaces.
    https://docs.unity3d.com/Manual/nav-AreasAndCosts.html
     
  5. VayneVerso

    VayneVerso

    Joined:
    Dec 5, 2022
    Posts:
    18
    Well, of course I've read this page, and I think I understand all of this, but all of the examples I've seen have been with regard to scenes built on a flat plane with objects. Water, then, is just some object that you place down that you flag in the inspector, and the NavMesh baker understands to treat it differently.

    I've never found any examples where it is explained how this might be extended to terrains built using the terrain editor. See, for example, the image I attached in my initial post. Perhaps I don't understand enough about Unity or terrain editing yet, but I don't know of a way of isolating the central "road" area, for example, and declaring it as a different type of Area for the NavMesh baker to take into consideration.

    It's not that big of a deal. I can work around it. I was just wondering.
     
    Last edited: Jan 21, 2023
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  7. VayneVerso

    VayneVerso

    Joined:
    Dec 5, 2022
    Posts:
    18
    Oh, that's fascinating. I didn't know about those tools. Thanks!

    My terrain is probably going to be fairly massive, but I think for longer roads, I could probably manually place some volumes around to incentivize road usage. I'll give it a shot!

    Edit: It works. I notice these are experimental features, though. Anybody know if these are going to be ready for primetime soon?
     
    Last edited: Jan 22, 2023
    Olmi likes this.
  8. Ng0ns

    Ng0ns

    Joined:
    Jun 21, 2016
    Posts:
    197
    Why not use splines? You could simply query to find if any are nearby, and with a bit of logic use the points for routing. You could even just store the points positions in some structure, like a kd-tree - then create some functionality for finding the nearest point based on the direction the character wish to go.
    Not a pro, but that's likely how I would go about doing it.
     
  9. VayneVerso

    VayneVerso

    Joined:
    Dec 5, 2022
    Posts:
    18
    Thanks for the suggestion. To be honest, I need to study up on splines (and many other things), because I could see them being pretty useful for things like NPC patrols or routes between game world locations. I'm thinking more about unplanned movement, though.

    E.g., an NPC spots another NPC on the other side of the woods; how does it get there? It might actually be more economical to close the distance by moving laterally to a road where it can travel at a faster speed. But it might not be. So I don't want to force them to use the road when it would be more costly.

    And of course, there would be no point in making them simply approach the road, either, because they would need to actually travel on it in order to reap any benefit from it.

    Additionally, if the road is a separate Area in the mesh, it allows me to sample the path position and adjust agent speed dynamically so that the result of the pathfinding algorithm actually makes sense.

    But there is, to be sure, a lot I still don't know. You may not be a pro, but I'm a novice at best.
     
  10. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    In regards to the "I want a mesh EXACTLY where I painted the texture" thing...

    Would ProBuilder be a solution? After all, we'd be making the meshes as simple as possible, we don't care about industrial precision, and it would allow us to draw 3D meshes on the existing terrain. Slap a MeshCollider on it and perhaps disable the whole thing post bake for performance? Just a thought.