PathfindingService 用於在兩個點之間找到合理的路徑,確保角色可以在點之間移動,而不會碰到牆壁或其他障礙。 預設路徑最短,但您可以實現路徑修改器來在各種材料、定義區域或通過障礙物計算更聰明的路徑。
參閱 角色路徑探索 了解使用方式。
概要
方法
在提供的兩個點之間找到 Path。
屬性
方法
CreatePath
建立基於各種代理參數的 Path 對象。有效的鑰匙和值在 agentParameters 表中如下所示:
<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>AgentCanClimb</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.PathfindingModifier|PathfindingModifiers</code> 和它們的 "cost" 對穿越。 有助於使代理對某些材料/區域比其他區域更喜歡。 請參閱 <a href="../../../characters/pathfinding.md#pathfinding-modifiers">這裡</a> 查看詳細資訊。</td></tr></tbody>
鑰匙 | 類型 | 預設 | 說明 |
---|
參數
可以微調路徑大小的 Lua 桌子 (會移動在路徑上的人形號)。 請參閱上方以取得有效的鑰匙、類型和說明。
返回
範例程式碼
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.
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 將是 1> Enum.PathStatus.NoPath1> 。
要取得 Path 對物件的方向點,您可以使用 Path:GetWaypoints() 函數。