Path
Path objects store the result of paths created by PathfindingService:CreatePath().
Once a path object is created, you can call Path:ComputeAsync() with a starting point and ending point. This will attempt to compute a valid path for a character to move along, based on default or custom parameters passed to CreatePath(). If ComputeAsync() successfully finds a path, the Path object will have a Path.Status value of Enum.PathStatus.Success. Otherwise the status will be Enum.PathStatus.NoPath which can occur if there are obstacles between the two points (and no way around) or if the points are inside of solid objects.
In addition to ComputeAsync(), Path objects have the GetWaypoints() method which returns a list of waypoints representing the points a character should follow in sequence to get from the beginning to the end of the path.
Finally, Path objects can be connected to the Path.Blocked event. This event will fire if, at any time during the path's existence, the path is blocked. Note that this can occur behind a character moving along the path, not just in front of it.
요약
속성
The success of the generated Path.
메서드
Returns an array of points in the path.
Checks if a path is blocked starting at a specific waypoint.
Computes a Path from a start position to an end position.
이벤트
Fires when the computed path becomes blocked.
Fires when a computed path that was blocked becomes unblocked.
속성
메서드
GetWaypoints
This function returns an array of all the PathWaypoints in a Path, as computed by Path:ComputeAsync().
Each waypoint in the array specifies a Vector3 position and action to take when this PathWaypoint is reached. The array is arranged in the order of waypoints from the path start to path end.
If a path could not be computed, this function will return an empty array.
반환
An array of PathWaypoints ordered from path start to path end.
코드 샘플
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local PATH_START = Vector3.new(0, 1, 0)
local PATH_END = Vector3.new(100, 1, 25)
path:ComputeAsync(PATH_START, PATH_END)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
print(waypoint.Position)
end
end
CheckOcclusionAsync
This function checks if a path is blocked starting at the waypoint indicated by start.
It returns the first waypoint of occlusion if blocked, -1 if not. it returns an error if start is less than 0 or greater than the number of waypoints in the Path.
매개 변수
반환
ComputeAsync
This function computes a Path from a start position to an end position. This function is not automatically called when a path is created and must be invoked each time the path needs to be updated.
Once the Path is computed, it will have a series of waypoints that, when followed, can lead a character along the path. These points are gathered with the Path:GetWaypoints() function.
매개 변수
The world position where the computed path begins.
The world position where the computed path finishes.
반환
코드 샘플
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)
-- When the path is blocked...
local function OnPathBlocked(blockedWaypointIdx)
-- Check if the obstacle is further down the path
if blockedWaypointIdx > currentWaypointIdx then
-- Recompute the path
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- Retrieve update waypoint list with path:GetWaypoints()
-- and Continue walking towards target
else
-- Error, path not found
end
end
end
path.Blocked:Connect(OnPathBlocked)
이벤트
Blocked
Fires when the computed path becomes blocked. Note that paths may become blocked somewhere behind the agent, such as a pile of rubble falling on a path as the agent runs away. See Handling Blocked Paths for details on checking the forward waypoint progress of an agent along a path.
매개 변수
Unblocked
Fires when a computed path that was blocked becomes unblocked. Note that a blocked path may become unblocked somewhere behind the agent, effectively making reaction to this event unnecessary. See Handling Blocked Paths for details on checking the forward waypoint progress of an agent along a path.