PathfindingService
Afficher les obsolètes
*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.
PathfindingService est utilisé pour trouver des chemins logiques entre deux points, afin que les caractères puissent se déplacer entre les points sans se heurter à des murs ou à d'autres obstacles.Par défaut, le chemin le plus court est calculé, mais vous pouvez implémenter des modifieurs de recherche de chemin pour calculer des chemins plus intelligents à travers différents matériaux, autour de régions définies ou à travers des obstacles.
Voir recherche de chemin de caractère pour les détails d'utilisation.
Résumé
Méthodes
Trouve un Path entre les deux points fournis.
Propriétés
Méthodes
CreatePath
Paramètres
Valeur par défaut : "nil"
Retours
Échantillons de code
Creating a Path with Pathfinding Service
local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- This model contains a start, end and three paths between the player can walk on: Snow, Metal and LeafyGrass
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- Create a path that avoids Snow and Metal materials
-- This will ensure the path created avoids the Snow and Metal paths and guides
-- the user towards the LeafyGrass path
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- Confirm the computation was successful
if success and path.Status == Enum.PathStatus.Success then
-- For each waypoint, create a part to visualize the path
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(`Path unable to be computed, error: {errorMessage}`)
end