Pathfinding
Creating a Path
Part 1
Moving Objects on Path
Part 2
Moving NPCs
Part 3
Jumping over Objects
Part 4
Illustrate the Path
local pathParts = game.Workspace.Path
local start = game.Workspace.Start
local finish = game.Workspace.Finish
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, finish.Position)
-- Get the path waypoints
local waypoints = path:GetWaypoints()
pathParts:ClearAllChildren()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = pathParts
end
Move Part on Path
local pathParts = game.Workspace.Path
local start = game.Workspace.Start
local finish = game.Workspace.Finish
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, finish.Position)
-- Get the path waypoints
local waypoints = path:GetWaypoints()
pathParts:ClearAllChildren()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = pathParts
start.Position = waypoint.Position
wait(0.1)
end
Redraw Path
local pathParts = game.Workspace.Path
local start = game.Workspace.Start
local finish = game.Workspace.Finish
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, finish.Position)
-- Get the path waypoints
local waypoints = path:GetWaypoints()
pathParts:ClearAllChildren()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = pathParts
end
wait(0.1)
end
Object Tracking
local start = game.Workspace.Start
local finish = game.Workspace.Finish
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, finish.Position)
-- Get the path waypoints
local waypoints = path:GetWaypoints()
-- Loop through waypoints
for i = 1, 5 do
if i <= #waypoints then
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoints[i].Position
part.Anchored = true
part.CanCollide = false
part.Parent = pathParts
start.Position = waypoints[i].Position
wait(.1)
end
end
end
Moving Humanoid Models
local pathParts = game.Workspace.Path
local start = game.Workspace.Start
local finish = game.Workspace.Finish
local dummy = game.Workspace.Dummy
local humanoid = dummy.Humanoid
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
path:ComputeAsync(dummy.Torso.Position, finish.Position)
-- Get the path waypoints
local waypoints = path:GetWaypoints()
pathParts:ClearAllChildren()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = pathParts
end
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
Waypoint Jump Action
if waypoint.Action == Enum.PathWaypointAction.Jump then
humaoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Humanoid Model Object Tracking
local pathParts = game.Workspace.Path
local finish = game.Workspace.Finish
local dummy = game.Workspace.Dummy
local humaoid = dummy.Humanoid
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(dummy.Torso.Position, finish.Position)
local waypoints = path:GetWaypoints()
for i = 1, 5 do
if i <= #waypoints then
if waypoints[i].Action == Enum.PathWaypointAction.Jump then
humaoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humaoid:MoveTo(waypoints[i].Position)
humaoid.MoveToFinished:Wait()
end
end
end