Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
NavigationAgent3D
實驗性: This class may be changed or removed in future versions.
用於尋路至某個位置並且能夠躲避障礙物的 3D 代理。
說明
用於尋路至某個位置並且能夠躲避靜態和動態障礙物的 3D 代理。父節點能夠使用計算結果沿著路徑動態前進。需要有導航資料才能正常工作。
躲避動態障礙物使用的是 RVO 防撞算法。避障的計算發生在物理之前,因此尋路資訊能夠在物理迭代時安全使用。
注意:設定 target_position 屬性之後,必須在每個物理影格使用一次 get_next_path_position() 函式來更新導航代理的內部路徑邏輯。這個函式回傳的向量位置應該用作該代理的父節點的下一次移動位置。
注意:這個類的 get_next_path_position() 等方法會觸發新的路徑計算。在代理的 waypoint_reached 等訊號中呼叫這些方法會造成無限遞歸。建議在物理步驟中呼叫這些方法,也可以推遲到目前影格的末尾再呼叫(見 Object.call_deferred() 或 Object.CONNECT_DEFERRED)。
教學
屬性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
BitField[PathMetadataFlags] |
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
方法
distance_to_target() const |
|
get_avoidance_layer_value(layer_number: int) const |
|
get_avoidance_mask_value(mask_number: int) const |
|
get_current_navigation_path() const |
|
get_current_navigation_result() const |
|
get_navigation_layer_value(layer_number: int) const |
|
get_navigation_map() const |
|
get_path_length() const |
|
get_rid() const |
|
is_target_reached() const |
|
void |
set_avoidance_layer_value(layer_number: int, value: bool) |
void |
set_avoidance_mask_value(mask_number: int, value: bool) |
void |
set_navigation_layer_value(layer_number: int, value: bool) |
void |
set_navigation_map(navigation_map: RID) |
void |
set_velocity_forced(velocity: Vector3) |
訊號
link_reached(details: Dictionary) 🔗
表示代理到達導航連結的訊號。當代理移動到路徑下一個位置的 path_desired_distance 範圍內,且該位置是導航連結時發出。
根據 path_metadata_flags 的值,詳細資訊字典可能包含以下鍵:
position:到達的連結的起始位置。rid:連結的 RID。owner:管理該連結的物件(通常是NavigationLink3D)。link_entry_position:如果owner可用且該所有者是一個 NavigationLink3D,它將包含代理正在進入時的連結點的全域位置。link_exit_position:如果owner可用且該所有者是一個 NavigationLink3D,它將包含代理正在退出時的連結點的全域位置。
navigation_finished() 🔗
表示代理導航完成的訊號。如果目標可達,則導航會在抵達目標位置時停止。如果目標不可達,則導航會在抵達最後一個路徑點時停止。每次載入的路徑只會發出一次這個訊號。
目標可達時,訊號會在 target_reached 後發出。
path_changed() 🔗
當代理程式必須更新已載入的路徑時發出:
因為路徑以前是空的。
因為導覽地圖已經改變。
因為代理推離目前路徑段比 path_max_distance 更遠。
target_reached() 🔗
表示代理到達目標的訊號,即代理移到了與 target_position 相距 target_desired_distance 之內的位置。每次載入路徑只會發出一次這個訊號。
目標可達時,訊號會在 navigation_finished 前發出。
並不是每次都能夠到達目標位置,但是每次都能夠到達最終位置。見 get_final_position()。
velocity_computed(safe_velocity: Vector3) 🔗
計算出避障速度時發出通知。只要 avoidance_enabled 為 true 並且代理存在導航地圖,就會在每次更新時發出。
waypoint_reached(details: Dictionary) 🔗
表示代理已到達航路點的訊號。當代理移動到路徑下一個位置的 path_desired_distance 範圍內時發出。
根據 path_metadata_flags 的值,詳細資訊字典可能包含以下鍵:
position:到達的路標點的位置。type:包含該路標的導航基元(區塊或連結)的型別。rid:包含的導航基元(區塊或連結)的 RID。owner:管理包含的導航基元(區塊或連結)的物件。
屬性說明
bool avoidance_enabled = false 🔗
如果為 true,該代理會在 NavigationServer3D 上註冊 RVO 避障回呼。當設定 velocity 並且處理完成時,會通過與 velocity_computed 的訊號連接接收到安全速度 safe_velocity Vector3。註冊的代理過多會為避障處理帶來顯著的效能開銷,應該僅在需要它的代理上啟用。
決定該 NavigationAgent 避障層的位域。avoidance_mask 中該位域存在交集的其他代理會躲避這個代理。
決定該 NavigationAgent 會躲避那些代理和障礙物的位元域,需要該位元域與對方的 avoidance_layers 存在至少一個共同的位元位。
float avoidance_priority = 1.0 🔗
該代理不會針對 avoidance_mask 存在配對但 avoidance_priority 更低的代理調整速度。相應地,優先順序更低的代理則會對其速度進行更大的調整,從而避免與這個代理發生碰撞。
如果為 true,則為該代理顯示除錯內容。
Color debug_path_custom_color = Color(1, 1, 1, 1) 🔗
如果 debug_use_custom 為 true,則該代理使用該顏色,不使用全域顏色。
float debug_path_custom_point_size = 4.0 🔗
如果 debug_use_custom 為 true,則該代理使用該柵格化點尺寸進行路徑點的渲染,不使用全域點尺寸。
bool debug_use_custom = false 🔗
如果為 true,則該代理使用 debug_path_custom_color 中定義的顏色,不使用全域顏色。
避障代理的高度。2D 避障時,代理會忽略位於其上方或低於目前位置 + 高度的其他代理或障礙物。3D 避障時只使用半徑球體,該設定無效。
如果為 true,並且代理使用 2D 避障,它將記住設定的 y 軸速度並在避障步進後重新應用它。雖然 2D 避障沒有 y 軸並在平坦平面上進行模擬,但該設定可以協助柔化不均勻 3D 幾何體上最明顯的裁剪。
該代理所需考慮的最大鄰居數。
代理所能達到的最大移動速度。
決定該代理計算路徑所使用的導覽地區導覽層的位元域。運作時進行修改會清空目前的導航路徑,並根據新的導覽層生成一條新的路徑。
float neighbor_distance = 50.0 🔗
搜尋其他代理的距離。
float path_desired_distance = 1.0 🔗
距離閾值,用於確定是否已到達某個路徑點。使用這個值,代理就不必精確地到達某個路徑點,只需到達該路徑點的大致區域內即可。如果這個值設得太大,該 NavigationAgent 會跳過路徑上的點,這可能導致它離開該導覽網格。如果這個值設得太小,該 NavigationAgent 會陷入重新尋路的死循環,因為它會在每次物理影格更新後都會超過下一個點。
float path_height_offset = 0.0 🔗
這個 NavigationAgent 的任何向量路徑位置的 Y 座標值都會減去這個高度偏移量。NavigationAgent 的高度偏移量既不會改變也不會影響導覽網格和尋路結果。要支援不同大小的代理,需要提供其他使用了帶有導覽網格區塊的導覽地圖,並且開發者使用合適的代理半徑或高度對其進行了烘焙。
float path_max_distance = 5.0 🔗
允許代理偏離通往最終位置的理想路徑的最大距離。可能為了防撞而產生偏離。超出最大距離時,會重新計算理想路徑。
BitField[PathMetadataFlags] path_metadata_flags = 7 🔗
void set_path_metadata_flags(value: BitField[PathMetadataFlags])
BitField[PathMetadataFlags] get_path_metadata_flags()
與導覽路徑一起返回的附加資訊。
PathPostProcessing path_postprocessing = 0 🔗
void set_path_postprocessing(value: PathPostProcessing)
PathPostProcessing get_path_postprocessing()
對 pathfinding_algorithm 找到的原始路徑走廊套用的路徑後期處理。
float path_return_max_length = 0.0 🔗
返迴路徑允許的最大長度,單位為世界單位。路徑超出該長度後會被截斷。
float path_return_max_radius = 0.0 🔗
返迴路徑從起點開始所允許的最大半徑,單位為世界單位。路徑超出該半徑後會被截斷。與 path_return_max_length 相比,當代理需要繞過拐角時,該屬效能夠允許代理走得更遠。
注意:該屬性會執行球面裁剪,只會考慮實際的導覽網格路徑點,路徑上的第一個位置是球體中心。
float path_search_max_distance = 0.0 🔗
搜尋通往目的位置多邊形的路徑時(可能不可達或非常遠),取消尋路前能夠搜尋的多邊形的最大距離。取消時,尋路會重設,建置出的路徑從起始多邊形開始,通往目前所找到的最接近目的位置的多邊形。小於等於 0 表示不限制數量,尋路時會搜尋與起始多邊形相連的所有多邊形,直到發現目的位置多邊形或已經查過所有待查的多邊形。
int path_search_max_polygons = 4096 🔗
搜尋通往目的位置多邊形的路徑時(可能不可達或非常遠),取消尋路前能夠搜尋的最大多邊形數。取消時,尋路會重設,建置出的路徑從起始多邊形開始,通往目前所找到的最接近目的位置的多邊形。小於等於 0 表示不限制數量,尋路時會搜尋與起始多邊形相連的所有多邊形,直到發現目的位置多邊形或已經查過所有待查的多邊形。
PathfindingAlgorithm pathfinding_algorithm = 0 🔗
void set_pathfinding_algorithm(value: PathfindingAlgorithm)
PathfindingAlgorithm get_pathfinding_algorithm()
路徑查詢中使用的尋路演算法。
該避障代理的半徑。這是該避障代理的“身體”,不是避障機制的起始半徑(由 neighbor_distance 控制)。
不會影響正常的尋路。要修改角色的尋路半徑,請在烘焙 NavigationMesh 資源時使用不同的 NavigationMesh.agent_radius 屬性,針對不同的角色大小使用不同的導覽地圖。
float simplify_epsilon = 0.0 🔗
The path simplification amount in world units.
如果為 true,將返迴路徑的簡化版本,其中移除了不太重要的路徑點。簡化量由 simplify_epsilon 控制。簡化使用 Ramer-Douglas-Peucker 算法的變體進行曲線點抽取。
路徑簡化有助於緩解使用某些代理型別和腳本行為可能出現的各種路徑追蹤問題。例如“開放場”中的“轉向”代理或避讓。
float target_desired_distance = 1.0 🔗
與目標的距離閾值,小於該閾值時會認為已抵達目標位置。已抵達目標位置時會發出 target_reached 並結束導航(見 is_navigation_finished() 和 navigation_finished)。
將這個屬性設定為大於 path_desired_distance 的值可以提前結束導航(導航會在到達最後一個路徑點之前停止)。
將這個屬性設定為小於 path_desired_distance 的值則會讓導航在更接近目標位置的地方結束(導航在抵達最後一個路徑點後不會立即停止)。不過如果設得太小,代理就會陷入重新移動的循環,因為每次物理影格更新時移動的距離都會超過與目標的實際距離。
Vector3 target_position = Vector3(0, 0, 0) 🔗
設定後,會向 NavigationServer 請求一條新的從目前代理位置到 target_position 的導覽路徑。
float time_horizon_agents = 1.0 🔗
The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but less freedom in choosing its velocities. A too high value will slow down agent movement considerably. Must be positive.
float time_horizon_obstacles = 0.0 🔗
The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to static avoidance obstacles. The larger the number, the sooner the agent will respond to static avoidance obstacles, but less freedom in choosing its velocities. A too high value will slow down agent movement considerably. Must be positive.
bool use_3d_avoidance = false 🔗
如果為 true,則代理會在 3D 空間中計算全向的避障速度,例如發生在空中、水下、太空中的遊戲。使用 3D 避障的代理只會躲避其他使用 3D 避障的代理、對基於半徑的障礙物作出反應。會忽略基於頂點的障礙物。
如果為 false,則代理會在 2D 空間中沿 X 和 Z 軸計算避障速度,忽略 Y 軸。使用 2D 避障的代理只會躲避其他使用 2D 避障的代理、對基於半徑和基於頂點的障礙物作出反應。其他使用 2D 避障的代理如果在該代理之下,或者高於該代理目前位置與 height 之和則會被忽略。
Vector3 velocity = Vector3(0, 0, 0) 🔗
為代理設定新的需求速度。避障模擬會盡可能嘗試滿足這個速度,但為了躲避與其他代理和障礙物的碰撞也會對它進行修改。將代理傳送至新的位置時,請使用 set_velocity_forced() 重設內部模擬速度。
方法說明
float distance_to_target() const 🔗
返回與目標位置的距離,使用的是代理的全域位置。使用者必須設定 target_position 才能獲得精確結果。
bool get_avoidance_layer_value(layer_number: int) const 🔗
返回 avoidance_layers 位元遮罩中指定的層是否啟用,給定的 layer_number 應在 1 和 32 之間。
bool get_avoidance_mask_value(mask_number: int) const 🔗
返回 avoidance_mask 位元遮罩中指定的遮罩是否啟用,給定的 mask_number 應在 1 和 32 之間。
PackedVector3Array get_current_navigation_path() const 🔗
Returns this agent's current path from start to finish in global coordinates. The path only updates when the target position is changed or the agent requires a repath. The path array is not intended to be used in direct path movement as the agent has its own internal path logic that would get corrupted by changing the path array manually. Use the intended get_next_path_position() once every physics frame to receive the next path point for the agent's movement as this function also updates the internal path logic.
int get_current_navigation_path_index() const 🔗
返回該代理目前位於導覽路徑 PackedVector3Array 中的哪一個索引。
NavigationPathQueryResult3D get_current_navigation_result() const 🔗
返回該代理目前正在使用的路徑所對應的路徑查詢結果。
Vector3 get_final_position() 🔗
回傳目前導航路徑上可到達的最終位置的全域座標。如果該代理需要更新導航路徑,從而使該代理髮出 path_changed 訊號,則該位置可能會發生變化。
bool get_navigation_layer_value(layer_number: int) const 🔗
返回 navigation_layers 位元遮罩中指定的層是否啟用,給定的 layer_number 應在 1 和 32 之間。
RID get_navigation_map() const 🔗
返回這個 NavigationAgent 節點的導覽地圖的 RID。這個函式返回的始終是在 NavigationAgent 上設定的地圖,不是 NavigationServer 上的抽象代理所使用的地圖。如果通過 NavigationServer API 修改了代理的地圖,該 NavigationAgent 節點是不會感知到地圖的變化的。請使用 set_navigation_map() 修改該 NavigationAgent 的導覽地圖,能夠同時在 NavigationServer 上的代理。
Vector3 get_next_path_position() 🔗
返回可以移動至的下一個位置,使用全域座標,確保中途沒有靜態物件的阻擋。如果該代理沒有導覽路徑,則會返回該代理父節點的位置。這個函式每個物理影格都必須呼叫一次,更新 NavigationAgent 內部的路徑邏輯。
float get_path_length() const 🔗
回傳目前計算得到的路徑的長度。如果仍在計算路徑或尚未請求計算,則回傳值為 0.0。
返回這個代理在 NavigationServer3D 上的 RID。
bool is_navigation_finished() 🔗
如果代理的導航已完成,則回傳 true。如果目標可達,則導航將在達到目標時結束。如果目標不可達,則導航將在到達路徑的最後一個航路點時結束。
注意:雖然 true 更喜歡停止呼叫更新函式,例如 get_next_path_position()。這避免了由於呼叫重複的路徑更新而使常設代理抖動。
如果 get_final_position() 位於 target_position 的 target_desired_distance 範圍內,則回傳 true。
bool is_target_reached() const 🔗
如果代理到達目標,即代理移動到 target_position 的 target_desired_distance 範圍內,則回傳 true。不一定總能到達目標,但應總能到達最終位置。請參閱 get_final_position()。
void set_avoidance_layer_value(layer_number: int, value: bool) 🔗
根據 value 啟用或停用 avoidance_layers 位元遮罩中指定的層,給定的 layer_number 應在 1 和 32 之間。
void set_avoidance_mask_value(mask_number: int, value: bool) 🔗
根據 value 啟用或停用 avoidance_mask 位元遮罩中指定的遮罩,給定的 mask_number 應在 1 和 32 之間。
void set_navigation_layer_value(layer_number: int, value: bool) 🔗
根據 value,啟用或停用 navigation_layers 位元遮罩中指定的層,給定的 layer_number 應在 1 和 32 之間。
void set_navigation_map(navigation_map: RID) 🔗
設定這個 NavigationAgent 節點所應使用的導覽地圖的 RID,同時還會更新 NavigationServer 上的代理 agent。
void set_velocity_forced(velocity: Vector3) 🔗
將防撞模擬的內部速度替換為 velocity。代理傳送到新的位置之後,應該在同一影格裡使用這個函式。如果頻繁呼叫這個函式,可能會讓代理卡住。