Un service qui calcule des chemins.
Concepts Clés | Détails |
---|---|
PathfindingService | Un service qui calcule des chemins. |
Path | Un objet représentant un chemin calculé. |
Waypoints | Points le long du chemin que le personnage suivra. |
Agent Parameters | Paramètres définissant les capacités de mouvement et les limitations du personnage (par exemple, rayon, hauteur, hauteur de saut). |
Blocked Event | Un événement qui se déclenche lorsque le chemin est bloqué. |
StatusChanged Event | Un événement qui se déclenche lorsque l'état du chemin change. |
Définir le Point de Départ et le Point d'Arrivée
Roblox Studio
local PathfindingService = game:GetService("PathfindingService")
local character = workspace:WaitForChild("NPC")
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Anchored = false
-- Get the NPC's start position
local startPoint
if character.PrimaryPart then
startPoint = character.PrimaryPart.Position
elseif character:FindFirstChild("HumanoidRootPart") then
startPoint = character.HumanoidRootPart.Position
else
error("No valid part found to determine the NPC's position")
end
local endPoint = workspace.End.Position
Créer un Chemin
-- Create a path object
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 10,
AgentMaxSlope = 45,
})
Calculer un Chemin
-- Compute the path
path:ComputeAsync(startPoint, endPoint)
-- Check the path's status
if path.Status == Enum.PathStatus.Success then
print("Path successfully computed.")
elseif path.Status == Enum.PathStatus.NoPath then
print("No path could be found.")
elseif path.Status == Enum.PathStatus.Fail then
print("Path computation failed.")
end
Visualiser le Chemin
-- Visualize the path
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Material = Enum.Material.Neon
part.Color = Color3.fromRGB(0, 255, 0)
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
end
Roblox Studio
Déplacer un PNJ le Long du Chemin
-- Move the character along the path
for _, waypoint in ipairs(waypoints) do
print(waypoint.Position)
print(humanoid)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
print("Character has followed the path.")
Roblox Studio
Exemple : Code Complet
local PathfindingService = game:GetService("PathfindingService")
local character = workspace:WaitForChild("NPC")
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Anchored = false
-- Get the NPC's start position
local startPoint
if character.PrimaryPart then
startPoint = character.PrimaryPart.Position
elseif character:FindFirstChild("HumanoidRootPart") then
startPoint = character.HumanoidRootPart.Position
else
error("No valid part found to determine the NPC's position")
end
local endPoint = workspace.End.Position
-- Create a path object
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 10,
AgentMaxSlope = 45,
})
-- Compute the path
path:ComputeAsync(startPoint, endPoint)
-- Check the path's status
if path.Status == Enum.PathStatus.Success then
print("Path successfully computed.")
elseif path.Status == Enum.PathStatus.NoPath then
print("No path could be found.")
elseif path.Status == Enum.PathStatus.Fail then
print("Path computation failed.")
end
-- Handle path being blocked during movement
path.Blocked:Connect(function(blockedWaypointIndex)
print("Path is blocked at waypoint index:", blockedWaypointIndex)
end)
-- Visualize the path
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Material = Enum.Material.Neon
part.Color = Color3.fromRGB(0, 255, 0)
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
end
-- Move the character along the path
for _, waypoint in ipairs(waypoints) do
print(waypoint.Position)
print(humanoid)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
print("Character has followed the path.")
Si ce tutoriel vous a été utile et que vous souhaitez soutenir mon travail, veuillez envisager de m'offrir un café.
Merci beaucoup pour votre soutien !
M'offrir un café