路径找到服务 用于找到两个点之间的逻辑路径,确保字符可以在两个点之间移动,而不会碰到墙壁或其他障碍物。默认情况下,计算最短路径,但您可以实现路径检索修改器,以在各种材料、定义区域或通过障碍等情况下计算更智能的路径。
请参阅角色路径查找获取使用详细信息。
概要
方法
找到两个提供点之间的 Path 。
属性
方法
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>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.PathfindingModified|PathfindingModifiers</code> 和其“成本”用于穿越。有助于使代理更喜欢某些材料/区域而不是其他材料/区域。请参阅<a href="/characters/pathfinding#pathfinding-modifiers">这里</a>获取详细信息。</td></tr></tbody>
关键 |
---|
参数
允许你精确调整代理的路径(将沿路移动的人形)的尺寸的 Luau 表。请参阅上面的有效键、类型和描述。
返回
代码示例
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 将是 Enum.PathStatus.NoPath 。
要获取 Path 对象的路径点,您可以使用 Path:GetWaypoints() 函数。