Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pusedo Code
- -- PathFinding
- --[[
- Create a Path that the AI could follow and when the Player joins The AI follows them
- ]]
- local AI = workspace.Dummy
- local AI_Humanoid = AI:WaitForChild("Humanoid")
- local AI_RootPart = AI:WaitForChild("HumanoidRootPart")
- AI_RootPart.Anchored = false
- local AI_Torso = AI:WaitForChild("UpperTorso")
- local PathFindingService = game:GetService("PathfindingService")
- game.Players.PlayerAdded:Connect(function(Player)
- local Player = Player
- local Character = Player.Character or Player.CharacterAdded:Wait()
- local Humanoid = Character:WaitForChild("Humanoid")
- local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
- local AgentPramaters = {
- AgentRadius = 5;
- AgentHeight = 10;
- AgentCanJump = true;
- WayPointDistance = 15;
- Costs = {
- -- Costs i want the AI to avoid this can be materials or a Part with a Pathfinding modifer
- }
- }
- local Path = PathFindingService:CreatePath(AgentPramaters)
- -- Path:ComputeAsync()
- Path:ComputeAsync(AI_Torso.Position, HumanoidRootPart.Position)
- --PathFindingService:FindPathAsync(AI_Torso.Position, HumanoidRootPart.Position)
- -- Check if the Path status is a sucess or not
- if Path.Status == Enum.PathStatus.Success then
- print("Sucessfully found path")
- local WayPoints = Path:GetWaypoints() -- Returns an Array of waypoints to loop through with the for loop
- -- Loop Through the WayPoints
- for _,Waypoint in ipairs(WayPoints) do
- -- Creating a node so i can see the way points computed
- local Node = Instance.new("Part")
- Node.BrickColor = BrickColor.new("Really black")
- Node.Position = Waypoint.Position + Vector3.new(0, 2, 0)
- Node.Material = Enum.Material.Neon
- Node.Size = Vector3.new(0.5, 0.5, 0,5)
- Node.Anchored = true
- Node.CanCollide = false
- Node.Shape = Enum.PartType.Ball
- Node.Parent = workspace
- if Waypoint.Action == Enum.PathWaypointAction.Jump then
- AI_Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
- end
- AI_Humanoid:MoveTo(Waypoint.Position)
- AI_Humanoid.MoveToFinished:Wait(2)
- print("The AI has finished moving to the Waypoint!")
- end
- Path.Blocked:Connect(function(BlockedIndexWayPoint)
- if BlockedIndexWayPoint > WayPoints then
- Path:CheckOcclusionAsync(AI_Torso.Position)
- print("The Path is Blocked")
- end
- Path.Unblocked:Connect(function(UnblockedIndexWayPoint)
- if UnblockedIndexWayPoint then
- print("The Waypoint has been unblocked")
- end
- end)
- end)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement