Advertisement
Anthony_5567

Roblox PathFinding

Jun 7th, 2022
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. -- Pusedo Code
  2.  
  3. -- PathFinding
  4. --[[
  5. Create a Path that the AI could follow and when the Player joins The AI follows them
  6. ]]
  7.  
  8. local AI = workspace.Dummy
  9.  
  10. local AI_Humanoid = AI:WaitForChild("Humanoid")
  11.  
  12. local AI_RootPart = AI:WaitForChild("HumanoidRootPart")
  13.  
  14. AI_RootPart.Anchored = false
  15.  
  16. local AI_Torso = AI:WaitForChild("UpperTorso")
  17.  
  18. local PathFindingService = game:GetService("PathfindingService")
  19.  
  20. game.Players.PlayerAdded:Connect(function(Player)
  21.    
  22.     local Player = Player
  23.    
  24.     local Character = Player.Character or Player.CharacterAdded:Wait()
  25.    
  26.     local Humanoid = Character:WaitForChild("Humanoid")
  27.    
  28.     local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
  29.    
  30.     local AgentPramaters = {
  31.         AgentRadius = 5;
  32.         AgentHeight = 10;
  33.         AgentCanJump = true;
  34.         WayPointDistance = 15;
  35.         Costs = {
  36.             -- Costs i want the AI to avoid this can be materials or  a Part with a Pathfinding modifer
  37.         }
  38.     }
  39.    
  40.     local Path = PathFindingService:CreatePath(AgentPramaters)
  41.    
  42.     -- Path:ComputeAsync()
  43.    
  44.     Path:ComputeAsync(AI_Torso.Position, HumanoidRootPart.Position)
  45.     --PathFindingService:FindPathAsync(AI_Torso.Position, HumanoidRootPart.Position)
  46.    
  47.     -- Check if the Path status is a sucess or not
  48.    
  49.     if Path.Status == Enum.PathStatus.Success then
  50.         print("Sucessfully found path")
  51.         local WayPoints = Path:GetWaypoints() -- Returns an Array of waypoints to loop through with the for loop
  52.         -- Loop Through the WayPoints
  53.        
  54.         for _,Waypoint in ipairs(WayPoints) do
  55.             -- Creating a node so i can see the way points computed
  56.            
  57.             local Node = Instance.new("Part")
  58.             Node.BrickColor = BrickColor.new("Really black")
  59.             Node.Position = Waypoint.Position + Vector3.new(0, 2, 0)
  60.             Node.Material = Enum.Material.Neon
  61.             Node.Size = Vector3.new(0.5, 0.5, 0,5)
  62.             Node.Anchored = true
  63.             Node.CanCollide = false
  64.             Node.Shape = Enum.PartType.Ball
  65.             Node.Parent = workspace
  66.            
  67.             if Waypoint.Action == Enum.PathWaypointAction.Jump then
  68.                 AI_Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
  69.             end
  70.             AI_Humanoid:MoveTo(Waypoint.Position)
  71.            
  72.             AI_Humanoid.MoveToFinished:Wait(2)
  73.            
  74.             print("The AI has finished moving to the Waypoint!")
  75.         end
  76.        
  77.         Path.Blocked:Connect(function(BlockedIndexWayPoint)
  78.             if BlockedIndexWayPoint > WayPoints then
  79.                 Path:CheckOcclusionAsync(AI_Torso.Position)
  80.                 print("The Path is Blocked")
  81.             end
  82.            
  83.             Path.Unblocked:Connect(function(UnblockedIndexWayPoint)
  84.                 if UnblockedIndexWayPoint then
  85.                     print("The Waypoint has been unblocked")
  86.                 end
  87.                
  88.             end)
  89.         end)
  90.     end
  91. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement