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> 和其“成本”对于穿越。 有助于使代理首选某些材料/区域过其他。 请参阅 <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 将为 2>Ennum.PathStatus.NoPath2>。
要获得 Path 对象的方向点,您可以使用 Path:GetWaypoints() 函数。