Skip to content

PathWaypoint ​

概览 ​

字段值
KindData · 数据 Data
Realmcommon

PathWaypoint 表示寻路结果中的单个途经路点,记录该点的世界坐标位置、到达时需要执行的动作类型以及可选的自定义标签。它通常成组出现,构成一条完整的移动路径,供角色或单位按顺序行进。

适用场景 ​

当单位需要沿计算出的路径移动时,遍历路点列表并依据每个路点的动作决定继续行走还是跳跃,从而驱动移动逻辑。

使用要点 ​

先通过 PathfindingService:CreatePath 创建路径,调用 path:ComputeAsync(startPosition, destination) 传入起点和终点;成功后用 path:GetWaypoints() 获取路点数组。

注意事项 ​

路点实例不可自行构造,PathWaypoint.New(...) 与 PathWaypoint(...) 均不可用,只能通过公开寻路 API 的返回值获取。比较 Action 时必须使用 Enums.PathWaypointAction 中的枚举项,不要使用数字。Position 为 Vector3,可直接参与位置计算或与单位当前位置进行比较。

代码示例 ​

从成功计算的路径中读取路点 ​

lua
-- @runtime client
local path = game:GetService('PathfindingService'):CreatePath({})
path:ComputeAsync(Vector3.New(0, 0, 0), Vector3.New(20, 0, 20))
if path.Status == Enums.PathStatus.Success then
    for index, waypoint in ipairs(path:GetWaypoints()) do
        print(index, waypoint.Position, waypoint.Action, waypoint.Label)
    end
end
path:Destroy()

属性 (3) ​

Name类型默认值说明
PositionVector3-PathWaypoint 的 Position 属性,类型为 Vector3,表示该路点在场景世界坐标系中的位置。该属性公开且可编辑,既可读取当前路点坐标,也可对它重新赋值以调整路点位置。
ActionPathWaypointAction-到达该路点时执行的动作类型,决定角色在该点的行为。
LabelString-路点的自定义标签,用于标识或分类路点。

关联类型 ​