Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This script demonstrates simple pathfinding.
- https://developer.roblox.com/articles/Pathfinding
- --]]
- local PathfindingService = game:GetService("PathfindingService")
- print("Variables for the zombie, its humanoid, and destination")
- local zombie = game.Workspace.Zombie
- local humanoid = zombie.Humanoid
- local points = game.Workspace.Points
- --Variables to store waypoints table and zombie's current waypoint
- local waypoints
- local currentWaypointIndex
- local function followPath(destinationObject)
- if destinationObject == nil then return end -- base case, thanks Dr. Burks for CSC 212!
- local path = PathfindingService:CreatePath()
- print("Create the path object")
- print("Compute and check the path")
- path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.Position)
- print("Empty waypoints table after each new path computation")
- waypoints = {}
- --recursive function
- local function onPathBlocked(blockedWaypointIndex, point)
- print("Check if the obstacle is further down the path")
- if blockedWaypointIndex > currentWaypointIndex then
- -- Call function to re-compute the path
- followPath(point)
- end
- end
- local function onWaypointReached(reached)
- if reached and currentWaypointIndex < #waypoints then
- currentWaypointIndex = currentWaypointIndex + 1
- humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
- end
- end
- print("Connect 'Blocked' event to the 'onPathBlocked' function")
- path.Blocked:Connect(onPathBlocked)
- print("Connect 'MoveToFinished' event to the 'onWaypointReached' function")
- humanoid.MoveToFinished:Connect(onWaypointReached)
- if path.Status == Enum.PathStatus.Success then
- print("Get the path waypoints and start zombie walking")
- waypoints = path:GetWaypoints()
- 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 = game.Workspace
- end
- print("Move to first waypoint")
- currentWaypointIndex = 1
- humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
- else
- print("Error (path not found); stop humanoid")
- humanoid:MoveTo(zombie.HumanoidRootPart.Position)
- end
- return true
- end
- local waitime = 10
- followPath(points.pointA)
- wait(waitime)
- followPath(points.pointB)
- wait(waitime)
- followPath(points.pointC)
- wait(waitime)
- followPath(points.pointD)
- wait(waitime)
- followPath(points.pointA)
- wait(waitime)
- followPath(points.pointC)
- wait(waitime)
- followPath(points.pointB)
- wait(waitime)
- followPath(points.pointD)
- print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement