Kelas Engine
PathfindingService
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
Rangkuman
Properti
Metode
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 |
Contoh Kode
Menggunakan Layanan Penemuan Jalur
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)
-- Ketika jalur terhalang...
local function OnPathBlocked(blockedWaypointIdx)
-- Periksa apakah rintangan lebih jauh di jalur
if blockedWaypointIdx > currentWaypointIdx then
-- Hitung ulang jalur
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- Ambil daftar titik waypoint yang diperbarui dengan path:GetWaypoints()
-- dan Lanjutkan berjalan menuju target
else
-- Kesalahan, jalur tidak ditemukan
end
end
end
path.Blocked:Connect(OnPathBlocked)Referensi API
Properti
EmptyCutoff
Metode
ComputeRawPathAsync
ComputeSmoothPathAsync
CreatePath
Parameter
| Nilai Default: "nil" |
Memberikan nilai
Contoh Kode
Membuat Jalur dengan Layanan Pathfinding
local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- Model ini berisi awal, akhir, dan tiga jalur yang dapat dilalui pemain: Salju, Logam, dan RumputBerdaun
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- Membuat jalur yang menghindari bahan Salju dan Logam
-- Ini akan memastikan jalur yang dibuat menghindari jalur Salju dan Logam dan mengarahkan
-- pengguna ke jalur RumputBerdaun
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- Hitung jalur
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- Konfirmasi perhitungan berhasil
if success and path.Status == Enum.PathStatus.Success then
-- Untuk setiap waypoint, buat bagian untuk memvisualisasikan jalur
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(`Jalur tidak dapat dihitung, kesalahan: {errorMessage}`)
endFindPathAsync