PathfindingService

顯示已棄用項目

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

無法建立
服務
未複製

路徑探索服務 用於在兩個點之間找到邏輯路徑,確保字符可以在墻壁或其他障礙物之間移動,而不會碰到墻壁或其他障礙物。預設情況下,計算最短路徑,但您可以實裝路徑檢索修改器來計算更聰明的路徑,在各種材料、環繞定義區域或穿過障礙物上。

請參閱角色路徑探索以獲得使用細節。

屬性

方法

CreatePath

創建基於各種代理參數的 Path 對象。有效鑰匙和值在 agentParameters 表中如下:


<th>類型</th>
<th>預設</th>
<th>說明</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>代理範圍</b></td>
<td>整數</td>
<td>2</td>
<td>決定空白空間被視為可穿越的最小需要量的垂直空間。</td>
</tr>
<tr>
<td><b>代理高度</b></td>
<td>整數</td>
<td>5</td>
<td>決定空白空間需要考慮通行的最小垂直空間量。</td>
</tr>
<tr>
<td><b>AgentCanJump</b></td>
<td>boolean</td>
<td>真的</td>
<td>決定是否允許在路徑探索期間跳躍。</td>
</tr>
<tr>
<td><b>代理可攀登</b></td>
<td>boolean</td>
<td>否</td>
<td>決定是否允許在路徑搜索期間攀登 <code>Class.TrussPart|TrussParts</code> 。</td>
</tr>
<tr>
<td><b>路徑距離</b></td>
<td>數字</td>
<td>4</td>
<td>決定路徑中間路點之間的間隔。</td>
</tr>
<tr>
<td><b>費用</b></td>
<td>表</td>
<td>{}</td>
<td>材料表或定義的 <code>Class.PathfindingModified|PathfindingModifiers</code> 和其"成本"用於穿越。有助於讓代理偏愛某些材料/區域,而不是其他材料/區域。請見 <a href="../../../characters/pathfinding.md#pathfinding-modifiers">這裡</a> 的詳情。</td>
</tr>
</tbody>
關鍵

參數

agentParameters: Dictionary

讓您精確調整代理的路徑尺寸(將沿路移動的人形)的 Luau 表。請參閱上面的有效鑰匙、類型和說明。

預設值:"nil"

返回

一個 Path 對物件。

範例程式碼

Demonstrates creating a path using PathfindingService:CreatePath()

'PathOptions' represents a Model with three paths made of different materials. When creating the path we define a large 'Cost' for two of the three paths materials. This will ensure the path created steers towards the path with the material of the lowest 'Cost'.

Once the path is created, small parts are created at each waypoint to visualize the path.

Creating a Path with Pathfinding Service

local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- This model contains a start, end and three paths between the player can walk on: Snow, Metal and LeafyGrass
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- Create a path that avoids Snow and Metal materials
-- This will ensure the path created avoids the Snow and Metal paths and guides
-- the user towards the LeafyGrass path
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- Confirm the computation was successful
if success and path.Status == Enum.PathStatus.Success then
-- For each waypoint, create a part to visualize the path
for _, waypoint in path:GetWaypoints() do
local part = Instance.new("Part")
part.Position = waypoint.Position
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Color = Color3.new(1, 0, 1)
part.Anchored = true
part.CanCollide = false
part.Parent = Workspace
end
else
print(`Path unable to be computed, error: {errorMessage}`)
end

FindPathAsync

暫停

此功能用於在兩個提供的點之間尋找 Path 。這條路徑使用由 PathfindingService 創建的導航網格,確保路徑可以被普通尺寸的 Roblox 角色跟隨。

此功能返回一個 Path 包含路徑坐標的對象。如果兩個點之間找不到路徑,這個函數仍然會返回一個 Path 物件,但該對物件的 Path.Status 將是 Enum.PathStatus.NoPath

若要取得 Path 對象的路徑物件,您可以使用 Path:GetWaypoints() 函數。

參數

start: Vector3

路徑開始坐標。

預設值:""
finish: Vector3

路徑完成坐標。

預設值:""

返回

一個 Path 對物件。

活動