Engine-Klasse
PathfindingService
*Dieser Inhalt wurde mit KI (Beta) übersetzt und kann Fehler enthalten. Um diese Seite auf Englisch zu sehen, klicke hier.
Zusammenfassung
Eigenschaften
Methoden
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 |
Code-Beispiele
Verwendung des Pfadfindungsdienstes
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)
-- Wenn der Pfad blockiert ist...
local function OnPathBlocked(blockedWaypointIdx)
-- Überprüfen, ob das Hindernis weiter unten auf dem Pfad ist
if blockedWaypointIdx > currentWaypointIdx then
-- Den Pfad neu berechnen
path:ComputeAsync(humanoidRootPart.Position, targetPosition)
if path.Status == Enum.PathStatus.Success then
-- Aktualisierte Wegpunktliste abrufen mit path:GetWaypoints()
-- und Weiter in Richtung Ziel gehen
else
-- Fehler, Pfad nicht gefunden
end
end
end
path.Blocked:Connect(OnPathBlocked)API-Referenz
Eigenschaften
EmptyCutoff
Methoden
ComputeRawPathAsync
ComputeSmoothPathAsync
CreatePath
Parameter
| Standardwert: "nil" |
Rückgaben
Code-Beispiele
Erstellen eines Pfades mit dem Pathfinding-Service
local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- Dieses Modell enthält einen Start-, Endpunkt und drei Pfade, auf denen der Spieler gehen kann: Schnee, Metall und Blattgras
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- Erstellen Sie einen Pfad, der Schnee- und Metallmaterialien ausweicht
-- Dies wird sicherstellen, dass der erstellte Pfad die Schnee- und Metallpfade meidet und
-- den Benutzer in Richtung des Blattgraspfades führt.
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- Berechnen Sie den Pfad
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- Bestätigen Sie, dass die Berechnung erfolgreich war
if success and path.Status == Enum.PathStatus.Success then
-- Für jeden Wegpunkt ein Teil erstellen, um den Pfad zu visualisieren
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(`Pfad konnte nicht berechnet werden, Fehler: {errorMessage}`)
endFindPathAsync