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

Question Open world ai navigation?

Discussion in 'Scripting' started by jessee03, Mar 26, 2023.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I've seen examples in unreal where ai is able to check their surroundings in an open world environment and calculate a path to walk. How can I mimic this in unity? I'm developing an open world game and generating navmesh would probably take days.. Anyone have any suggestions?
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    Whatever Unreal is doing is probably the same thing you'd do anywhere else. Pathfinding techniques used in games aren't magic, they are based on algorithmic solutions such as pathfinders and other graph traversal algorithms, which, depending on the complexity of the problem, employ various mathematical and computational concepts like binary heaps, priority queues, space partitioning, and whathaveyou, to optimally arrive at a (decent) solution.

    Navigation is also typically coupled with techniques that accelerate (SIMD) or parallelize (multithreading) the solvers, so that many agents can navigate your world efficiently. Also you mustn't forget that while having a path is one thing, you're also supposed to move your agents along these paths, and there are many tricks that can be employed to trade between the actual path quality and perceived movement quality. Also it matters whether the world has small obstacles (trees and rocks), or huge obstacles (seas and mountains), or if obstacles are regional (rivers) or even scarce.

    This is a very deep topic, that can be very specific to a particular project. It actually makes sense to design the game around such concepts, and not vice versa. I.e. before you decide you want to make an open world environment, you're supposed to decide on every design decision that stems from such technicalities, because it will affect from how the game is played, to how the NPCs behave, to how much time you'll need to develop it, and finally how it will perform and whether it scales or not.

    Making truly open worlds takes a lot of experience and deep knowledge on the subject, good tools, and tricks up one's sleeve. Otherwise, what's the point of an "open" world other than perhaps player exploration?

    Think Minecraft, it features pretty much localized and almost-random behaving mobs. This is for a reason. It is nearly impossible to make the mobs aware of the entire world, to explore and navigate it the way players do. Also consider any open world games that features a 2D map. Can a map represent the world for the purposes of navigation? If so, you can solve your pathfinding on a simplified map of the world. Minecraft cannot do this because it features a 3D world, but e.g. Far Cry or Elder Scrolls games can.

    You can also think in terms of solving some macro space (entire regions) vs micro space (what the player sees). You run a regional pathfinder to solve the connection between two huge regions instead of having to traverse each little tile. Now your NPC knows which regions will lead it to its destination. Any time an NPC enters a region, it then solves this space locally with a local pathfinder. It is definitely much more involved and you need to be capable of producing such complex systems, but the game is now capable of skimming through this space blazingly fast, handling potentially hundreds (or more) of busy agents.

    Maybe you use a grid-based A* for the local path, which runs very fast but looks way too regular. You can solve this by introducing steering behavior so that your agents have some freedom in how they wish to interpret this path, and therefore appear to move naturally because the path is only a suggestion. You can also make them have a capsule collider to make them slide against the small obstacles, but you do it only when they're on the screen, and deactivate this if they're not, to save on CPU. You can also look into boids, where you actually move entire groups at the same time, but position the individuals relative to each other if they can be seen.

    And this is only 1% of what you could do to make your world appear more alive, full of creatures, and truly open.

    tl;dr Look into pathfinders in general. Unity's navmesh is just one solution, and it's not the best or the most general, it's just something that solves 80% of the most typical use cases.