Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

3D navigation overview

Godot 提供了多種物件、類和伺服器,可幫助 2D 和 3D 遊戲實作基於柵格(Grid)或網格(Mesh)的導覽和尋路。下文將對 Godot 中與 3D 場景導覽相關的對象及其主要用途進行概述。

Godot 可匯入下列圖片格式:

  • Vector3

    Astar3D 物件能夠在由具有權重的**點**構成的圖中搜尋最短路徑。

    The AStar3D class is best suited for cell-based 3D gameplay that does not require actors to reach any possible position within an area but only predefined, distinct positions.

  • String

    NavigationServer3D provides a powerful server API to find the shortest path between two positions on an area defined by a navigation mesh.

    The NavigationServer is best suited for 3D realtime gameplay that does require actors to reach any possible position within a navigation mesh defined area. Mesh-based navigation scales well with large game worlds as a large area can often be defined with a single polygon when it would require many, many grid cells.

    The NavigationServer holds different navigation maps that each consist of regions that hold navigation mesh data. Agents can be placed on a map for avoidance calculation. RIDs are used to reference internal maps, regions, and agents when communicating with the server.

    NavigationServer 中可用的 RID 型別如下。
    • 導覽地圖 RID

      Reference to a specific navigation map that holds regions and agents. The map will attempt to join the navigation meshes of the regions by proximity. The map will synchronize regions and agents each physics frame.

    • 導覽區塊 RID

      Reference to a specific navigation region that can hold navigation mesh data. The region can be enabled / disabled or the use restricted with a navigation layer bitmask.

    • 導覽連結 RID

      引用指定的導覽連結,能夠將兩個導覽網格上的位置進行連接,無視距離。

    • 導覽代理 RID

      Reference to a specific avoidance agent. The avoidance is defined by a radius value.

    • NavObstacle RID

      Reference to a specific avoidance obstacle used to affect and constrain the avoidance velocity of agents.

The following scene tree nodes are available as helpers to work with the NavigationServer3D API.

  • String

    存放 Navigation Mesh 資源的節點,該資源定義的是 NavigationServer3D 中的導覽網格。

    • 區塊可以啟用/禁用。

    • The use in pathfinding can be further restricted through the navigation_layers bitmask.

    • The NavigationServer3D will join the navigation meshes of regions by proximity for a combined navigation mesh.

  • String

    A Node that connects two positions on navigation meshes over arbitrary distances for pathfinding.

    • 連結可以啟用/禁用。

    • 連結可以設為單向或雙向。

    • The use in pathfinding can be further restricted through the navigation_layers bitmask.

    連結會告訴尋路存在這樣的連接、相關的消耗如何。實際的代理處理以及移動需要在自訂腳本中實作。

  • String

    A helper Node used to facilitate common NavigationServer3D API calls for pathfinding and avoidance. Use this Node with a Node3D inheriting parent Node.

  • Dictionary

    A Node that can be used to affect and constrain the avoidance velocity of avoidance enabled agents. This Node does NOT affect the pathfinding of agents. You need to change the navigation meshes for that instead.

填寫下列設定:

  • String

    A resource that holds 3D navigation mesh data. It provides 3D geometry baking options to define navigation areas inside the Editor as well as at runtime.

    • NavigationRegion3D 節點使用該資源定義其導覽區域。

    • The NavigationServer3D uses this resource to update the navigation mesh of individual regions.

    • The GridMap Editor uses this resource when specific navigation meshes are defined for each grid cell.

也參考

除了這份說明文件,你可能也會想看看 Godot Demo 專案

匯入 3D 場景

The following steps show a basic setup for minimal viable navigation in 3D. It uses the NavigationServer3D and a NavigationAgent3D for path movement.

  1. 在場景中新增一個 NavigationRegion3D 節點。

  2. 按一下該區塊節點,向該節點新增一個新的 NavigationMesh 資源。

    ../../_images/nav_3d_min_setup_step1.png
  3. 將腳本附加到節點。

  4. 選中該 MeshInstance3D 節點,新增一個新的 PlaneMesh 並將其 XY 大小設為 10。

  5. 再次選中該區塊節點,點擊頂欄中的“烘焙導覽網格”按鈕。

    ../../_images/nav_3d_min_setup_step2.png
  6. Now a transparent navigation mesh appears that hovers some distance on top of the PlaneMesh.

    ../../_images/nav_3d_min_setup_step3.png
  7. 在場景中新增一個 CharacterBody3D 節點,設定基礎的碰撞形狀,新增一些網格方便觀察。

  8. 在該角色節點下新增一個 NavigationAgent3D 節點。

    ../../_images/nav_3d_min_setup_step4.webp
  9. 為 CharacterBody3D 節點新增一個腳本,內容如下。場景完全載入後,我們確保設定移動目標,NavigationServer 有時間進行同步。另外,新增一個 Camera3D、一些燈光以及環境,這樣才能夠看到東西。

extends CharacterBody3D

var movement_speed: float = 2.0
var movement_target_position: Vector3 = Vector3(-3.0,0.0,2.0)

@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D

func _ready():
    # These values need to be adjusted for the actor's speed
    # and the navigation layout.
    navigation_agent.path_desired_distance = 0.5
    navigation_agent.target_desired_distance = 0.5

    # Make sure to not await during _ready.
    call_deferred("actor_setup")

func actor_setup():
    # Wait for the first physics frame so the NavigationServer can sync.
    await get_tree().physics_frame

    # Now that the navigation map is no longer empty, set the movement target.
    set_movement_target(movement_target_position)

func set_movement_target(movement_target: Vector3):
    navigation_agent.set_target_position(movement_target)

func _physics_process(delta):
    if navigation_agent.is_navigation_finished():
        return

    var current_agent_position: Vector3 = global_position
    var next_path_position: Vector3 = navigation_agent.get_next_path_position()

    velocity = current_agent_position.direction_to(next_path_position) * movement_speed
    move_and_slide()

備註

On the first frame the NavigationServer map has not synchronized region data and any path query will return empty. Wait for the NavigationServer synchronization by awaiting one frame in the script.