Path

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
未複製

路徑 物件儲存由PathfindingService:CreatePath()的路徑結果。

一旦路徑對象被創建,您可以使用起點和終點呼叫 Path:ComputeAsync() 。這將嘗試根據預設或自訂參數傳送到 CreatePath() 來計算角色移動的有效路徑。如果 ComputeAsync() 成功找到路徑,Path 對象將有 Path.Status 值的 Enum.PathStatus.Success 。否則狀態將是 Enum.PathStatus.NoPath ,這可能發生在兩個點之間有障礙物(且無法繞過)或兩個點位於固體物體內。

除了 ComputeAsync() 之外,Path 物件還有 GetWaypoints() 方法,該方法返回一個列表代表角色應該按順序從路徑一端到另一端的點的路徑點。

最後,Path可以 連接Path.Blocked。此事件將在路徑存在期間的任何時間發生,如果路徑被阻止。請注意,這可能會在 個字符沿路移動的情況下發生 ,不僅限於前面。

概要

方法

活動

屬性

唯讀
未複製
平行讀取

生成的 Path 的成功。

方法

GetWaypoints

此功能返回一個 array 的所有 PathWaypointsPath 中,由 Path:ComputeAsync() 計算。

在 array 中的每個節點指定一個 Vector3 位置和 action 當此路徑節點達到時要採取的位置。陣列按路徑從起點到結束點的順序排列。

如果無法計算路徑,此函數將返回一個空陣列。


返回

從路徑開始到路徑結束的一個 array PathWaypoints

範例程式碼

The example below demonstrates how to create a Path and it's PathWaypoints using the PathService.

It tries to Path:ComputeAsync() a path between two Vector3 positions, from pathStart to pathEnd. If a path is successfully created, indicated by it's Enum.PathStatus, the code block gets an array of its waypoints using Path:GetWaypoints(). Then, it loops through the array and prints each waypoint's position.

Get Path Waypoints

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local PATH_START = Vector3.new(0, 1, 0)
local PATH_END = Vector3.new(100, 1, 25)
path:ComputeAsync(PATH_START, PATH_END)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
print(waypoint.Position)
end
end

CheckOcclusionAsync

暫停

此功能檢查路徑是否從指示的路點開始被阻塞。 start 指示的路點是路徑的起點。

如果被阻止,返回遮蔽的第一個路點;如果未被阻止,則返回 -1 路點。如果 開始 小於 0 或大於 Path 中的路點數量,將返回錯誤。

參數

start: number
預設值:""

返回

ComputeAsync

()
暫停

此功能會將Path。當創建路徑時,此功能不會自動呼叫,必須每次路徑需要更新時才能呼叫。

一旦路徑計算出來,它將有一系列路點,當遵循時,可以帶領角色沿路。這些點由 Path:GetWaypoints() 函數收集。

參數

start: Vector3

計算路徑開始的世界位置。

預設值:""
finish: Vector3

計算路徑結束的世界位置。

預設值:""

返回

()

範例程式碼

The code sample below explores how to move an NPC along a more complex path or around obstacles. This is known as pathfinding.

Using the Pathfinding Service

local PathfindingService = game:GetService("PathfindingService")
local agentParams = {
AgentRadius = 2.0,
AgentHeight = 5.0,
AgentCanJump = false,
}
local currentWaypointIdx = 1
local path = PathfindingService:CreatePath(agentParams)
local humanoidRootPart = script.Parent:FindFirstChild("HumanoidRootPart")
local targetPosition = Vector3.new(50, 0, 50)
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
-- When the path is blocked...
local function OnPathBlocked(blockedWaypointIdx)
-- Check if the obstacle is further down the path
if blockedWaypointIdx > currentWaypointIdx then
-- Recompute the path
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- Retrieve update waypoint list with path:GetWaypoints()
-- and Continue walking towards target
else
-- Error, path not found
end
end
end
path.Blocked:Connect(OnPathBlocked)

活動

Blocked

當計算路徑被阻塞時發生火災。請注意,路徑可能會在某處被阻塞,例如,當代理逃脫時,一堆垃圾落在路徑上。請參閱 處理阻塞路徑 以了解有關沿路檢查代理前進路徑進度的詳情。

參數

blockedWaypointIdx: number

Unblocked

當阻塞的計算路徑被解除阻塞時,發生火災。請注意,被阻塞的路徑可能會在某處 解鎖,使反應於此事件不再必要。請參閱 處理阻塞路徑 以了解有關沿路檢查代理前進路徑進度的詳情。

參數

unblockedWaypointIdx: number