Up to date

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

使用 NavigationRegion

NavigationRegions are the visual Node representation of a region of the navigation map on the NavigationServer. Each NavigationRegion node holds a resource for the navigation mesh data.

2D 和 3D 版本分別為 NavigationRegion2DNavigationRegion3D

各個NavigationRegions將其2D NavigationPolygon或3D NavigationMesh資源資料上傳到NavigationServer。導覽伺服器地圖將這些資訊轉換為組合導覽地圖以供尋路。

To create a navigation region using the scene tree add a NavigationRegion2D or NavigationRegion3D node to the scene. All regions require a navigation mesh resource to function. See Using navigation meshes to learn how to create and apply navigation meshes.

NavigationRegions will automatically push global_transform changes to the region on the NavigationServer which makes them suitable for moving platforms. The NavigationServer will attempt to connect the navigation meshes of individual regions when they are close enough. For more details see Connecting navigation meshes. To connect NavigationRegions over arbitrary distances see 使用 NavigationLink to learn how to create and use NavigationLinks.

警告

While changing the transform of a NavigationRegion node does update the region position on the NavigationServer, changing the scale does not. A navigation mesh resource has no scale and needs to be fully updated when source geometry changes scale.

可以啟用/停用區域,如果停用,將不會對未來的尋路查詢做出貢獻。

備註

當區域啟用/停用時,現有路徑不會自動更新。

建立內容

新的 NavigationRegion 節點將自動註冊到其 2D/3D 維度的預設世界導覽地圖。

然後可以使用 get_region_rid() 從 NavigationRegion 節點取得區域 RID。

extends NavigationRegion3D

var navigationserver_region_rid: RID = get_region_rid()

也可以使用 NavigationServer API 建立新區域並將其新增到任何現有地圖中。

如果直接使用 NavigationServer API 建立區域,則需要手動為其指派導覽地圖。

extends Node2D

var new_2d_region_rid: RID = NavigationServer2D.region_create()
var default_2d_map_rid: RID = get_world_2d().get_navigation_map()
NavigationServer2D.region_set_map(new_2d_region_rid, default_2d_map_rid)
extends Node3D

var new_3d_region_rid: RID = NavigationServer3D.region_create()
var default_3d_map_rid: RID = get_world_3d().get_navigation_map()
NavigationServer3D.region_set_map(new_3d_region_rid, default_3d_map_rid)

備註

NavigationRegions 只能分配給單一 NavigationMap。如果將現有區域分配給新地圖,它將保留舊地圖。