Real Time Navigation (3D)

Введение

Поиск пути в трёхмерной среде имеет решающее значение для многих игр, обычно это то, как персонажи или сущности, не управляемые напрямую, находят свой путь по окружению. Godot предоставляет несколько узлов для этой цели:

The map and navigation regions

The "map" is the entire world for navigation, it's similar to "space" for the physics engine. It's comprised of navigation regions, these regions define parts of the world that can be navigated around by navigation agents.

Для создания навигационной области добавьте узел NavigationMeshInstance в 3D-сцену. Затем в инспекторе для этой сетки создайте или добавьте NavigationMesh. Навмеш содержит опции для того, как он будет сгенерирован при запекании. Параметры геометрии контролируют, какие узлы и типы узлов используются для запекания сетки. Полное описание каждого параметра и его работы можно найти в справочнике по классу NavigationMesh.

Once the settings have been properly configured press the "Bake NavMesh" button at the top of the inspector to generate it.

../../_images/bake_navmesh.png

Примечание

It can also be generated at runtime using the bake_navigation_region() method of the navigation region node.

Once the mesh has finished generating you should see the transparent navigation mesh above the areas in the scene that can be navigated to.

../../_images/baked_navmesh.png

Keep in mind that the navmesh shows where the center of an entity can go. For example, if you set the agent radius to 0.5 then the navigation mesh will have a distance of 0.5 from any ledges or walls to prevent clipping into the wall or hanging off of the edge.

Navigation agents can moved from one region to another if they are next to each other. Additionally a baked navmesh can be moved at runtime and agents will still be able to navigate onto it from another region. For example, navigating onto a moving platform that has stopped will work.

RVO processing

RVO stands for reciprocal velocity obstacle. RVO processing is a way to pathfind while taking into account other agents and physics bodies that are also moving.

To use it set a target like normal. Then an agent needs to fetch its next nav path location, and compute its velocity to that location. Instead of using that value to move use it to set the velocity on the agent with set_velocity. Then a new velocity that takes into account other agents and obstacles is generated and emitted with the signal velocity_computed.

However agents can only take into account a set number of other nearby agents, this is the max neighbors property of an agent and can be adjusted. This is not a limit for how many agents can use a navigation region at the same time.

Generating a path (old method)

This is the old method for generating a navigation path, it will be removed in Godot 4. First, add a navigation node to the scene, then add a navigation mesh instance as it's child and set up a navigation mesh.

To get a path between two areas on a map you use the navigation node method get_simple_path(). The first argument is a Vector3 of the starting location, the next is a Vector3 of the end location. And the last argument is a boolean for whether or not agent properties of a navmesh are considered when generating a path.

The method will return a PoolVector3Array consisting of points that make a path. If there is no way to navigate to the end location the method will return a blank PoolVector3Array.