경로 개체는 PathfindingService:CreatePath() 생성된 경로의 결과를 저장합니다.
경로 개체가 생성되면 시작 지점과 종점을 사용하여 Path:ComputeAsync()를 호출할 수 있습니다.이는 기본 또는 사용자 지정 매개 변수를 통해 전달된 경로에 따라 캐릭터가 이동할 수 있는 유효한 경로를 계산하려고 시도합니다, CreatePath() .경로를 성공적으로 찾으면 개체에는 값의 가 있습니다.그렇지 않으면 상태가 Enum.PathStatus.NoPath 이 될 수 있으며, 두 지점 사이에 장애물이 있거나 지점이 단단한 개체 내에 있을 경우 발생할 수 있습니다.
ComputeAsync() 에 더해, Path 개체에는 경로의 시작부터 끝까지 캐릭터가 따라야 하는 지점을 나타내는 목록을 반환하는 GetWaypoints() 메서드가 있습니다.
마지막으로, Path 개체는 연결될 수 있습니다 이벤트에 대해 Path.Blocked 에 연결될 수 있습니다.이 이벤트는 경로가 존재하는 동안 언제든지 경로가 차단되면 발생합니다.이는 경로를 따라 이동하는 문자 뒤에서만 발생할 수 있으며, 앞에서만 발생하지는 않습니다. Note that this can occur behind a character moving along the path, not just in front of it.
요약
메서드
경로에서 포인트 배열을 반환합니다.
특정 웨이포인트부터 경로가 차단되는지 확인합니다.
시작 위치에서 끝 위치까지 Path 를 계산합니다.
이벤트
계산된 경로가 차단되면 발생합니다.
차단되었던 계산된 경로가 차단 해제되면 발생합니다.
속성
메서드
GetWaypoints
이 함수는 계산된 모든 를 포함하는 배열을 반환하며, 이는 에 의해 계산됩니다.
배열의 각 웨이포인트는 이 경로 웨이포인트에 도달할 때 Vector3 위치와 action 를 지정합니다.배열은 경로 시작에서 경로 종료웨이포인트 순서로 정렬됩니다.
경로를 계산할 수 없으면 이 함수는 빈 배열을 반환합니다.
반환
경로 시작에서 경로 종료순서가 지정된 배열 PathWaypoints 배열.
코드 샘플
The example below demonstrates how to create a Path and it's PathWaypoints using the PathService.
It tries to Path:ComputeAsync() a path between two Vector3 positions, from pathStart to pathEnd. If a path is successfully created, indicated by it's Enum.PathStatus, the code block gets an array of its waypoints using Path:GetWaypoints(). Then, it loops through the array and prints each waypoint's position.
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 .
차단되는 경우 최초의 가림점을 반환하고, 그렇지 않은 경우 -1을 반환합니다.it returns an error if start 이 0보다 작거나 Path 에 있는 웨이포인트 수보다 큰 경우에는 오류를 반환합니다.
매개 변수
반환
ComputeAsync
이 함수는 시작 위치에서 끝 위치까지 Path를 계산합니다.이 함수는 경로가 생성될 때 자동으로 호출되지 않으며 경로가 업데이트될 때마다 호출해야 합니다.
경로가 계산되면 경로를 따라가면 캐릭터를 이끌 수 있는 일련의 웨이포인트가 있습니다.이 포인트는 Path:GetWaypoints() 함수로 수집됩니다.
매개 변수
반환
코드 샘플
The code sample below explores how to move an NPC along a more complex path or around obstacles. This is known as pathfinding.
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
계산된 경로가 차단되면 발생합니다.경로가 에이전트가 도망가는 경로에서 쓰레기 더미처럼 떨어지면서 어딘가에 차단될 수 있다는 점에 유의하십시오. 뒤에 에이전트가 도망가는 경로에서 쓰레기 더미처럼 떨어지면서 어딘가에 차단될 수 있다는 점에 유의하십시오.차단된 경로 처리에 대한 자세한 내용은 경로를 따라 에이전트의 전방 웨이포인트 진행 상황을 확인하는 방법을 참조하십시오.