요약
메서드
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
-- 경로:GetWaypoints()로 업데이트된 웨이포인트 목록 가져오기
-- 그리고 목표를 향해 계속 걸어가세요
else
-- 오류, 경로를 찾을 수 없습니다
end
end
end
path.Blocked:Connect(OnPathBlocked)API 참조
속성
EmptyCutoff
메서드
ComputeRawPathAsync
ComputeSmoothPathAsync
CreatePath
매개 변수
| 기본값: "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}`)
endFindPathAsync