学习
引擎类
PathfindingService
无法创建
服务
未复制

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处


概要
属性
方法
ComputeRawPathAsync(start: Vector3,finish: Vector3,maxDistance: number):Path
已弃用
ComputeSmoothPathAsync(start: Vector3,finish: Vector3,maxDistance: number):Path
已弃用
CreatePath(agentParameters: Dictionary):Path
FindPathAsync(start: Vector3,finish: Vector3):Path
已弃用
继承成员
代码示例
使用路径寻找服务
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)
-- 当路径被阻塞时...
local function OnPathBlocked(blockedWaypointIdx)
-- 检查障碍物是否位于路径的后面
if blockedWaypointIdx > currentWaypointIdx then
-- 重新计算路径
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- 使用 path:GetWaypoints() 获取更新的路径点列表
-- 并继续朝着目标行走
else
-- 错误,未找到路径
end
end
end
path.Blocked:Connect(OnPathBlocked)

API 参考
属性
EmptyCutoff
已弃用

方法
ComputeRawPathAsync
已弃用

ComputeSmoothPathAsync
已弃用

CreatePath
功能: Basic
PathfindingService:CreatePath(agentParameters:Dictionary):Path
参数
agentParameters:Dictionary
默认值:"nil"
返回
代码示例
创建路径与路径寻找服务
local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- 此模型包含一个起点、终点以及玩家可以行走的三条路径:雪、金属和叶草
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- 创建一个避免雪和金属材料的路径
-- 这将确保创建的路径避免雪和金属路径,并引导
-- 用户朝向叶草路径
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- 计算路径
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- 确认计算成功
if success and path.Status == Enum.PathStatus.Success then
-- 对于每个航点,创建一个部分以可视化路径
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(`路径无法计算,错误:{errorMessage}`)
end

FindPathAsync
已弃用

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。