Up to date

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

2D navigation overview

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

Godot 可匯入下列圖片格式:

  • Vector2

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

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

  • String

    NavigationServer2D 提供了強大的伺服器 API,能夠在區域中搜尋兩個位置之間的最短路徑,區域由導覽網格定義。

    The NavigationServer is best suited for 2D 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 specified 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 NavigationServer2D API.

  • String

    存放 NavigationPolygon 資源的節點,該資源定義的是 NavigationServer2D 中的導覽網格。

    • 區塊可以啟用/禁用。

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

    • The NavigationServer2D 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 NavigationServer2D API calls for pathfinding and avoidance. Use this Node with a Node2D 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 2D navigation mesh data. It provides polygon drawing tools to allow defining navigation areas inside the Editor as well as at runtime.

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

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

    • TileSet 編輯器會定義圖塊的導覽區域時在內部建立並使用該資源。

2D 場景的設定

The following steps show the basic setup for minimal viable navigation in 2D. It uses the NavigationServer2D and a NavigationAgent2D for path movement.

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

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

    ../../_images/nav_2d_min_setup_step1.png
  3. 使用 NavigationPolygon 繪製工具定義可移動導覽區域。

    ../../_images/nav_2d_min_setup_step2.png

    備註

    The navigation mesh defines the area where an actor can stand and move with its center. Leave enough margin between the navigation polygon edges and collision objects to not get path following actors repeatedly stuck on collision.

  4. 在場景中新增一個 CharacterBody2D 節點,設定基礎的碰撞形狀,新增一個精靈或網格方便觀察。

  5. 在該角色節點下新增一個 NavigationAgent2D 節點。

    ../../_images/nav_2d_min_setup_step3.webp
  6. 為 CharacterBody3D 節點新增下面的腳本。場景完全載入後,我們確保設定移動目標,NavigationServer 有時間進行同步。

extends CharacterBody2D

var movement_speed: float = 200.0
var movement_target_position: Vector2 = Vector2(60.0,180.0)

@onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D

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

    # 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: Vector2):
    navigation_agent.target_position = movement_target

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

    var current_agent_position: Vector2 = global_position
    var next_path_position: Vector2 = 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.