Roblox Studio AI Pathfinding Tutorial || Part 4 - Chasing Player

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น • 2

  • @duckhive-games
    @duckhive-games  4 หลายเดือนก่อน

    Project File Here (FREE):
    www.patreon.com/posts/roblox-studio-ai-111845138?Link&

    • @duckhive-games
      @duckhive-games  4 หลายเดือนก่อน

      -- NPC Pathfinding Script (Server-Side) -- Place in NPC
      local PathfindingService = game:GetService("PathfindingService")
      local npc = script.Parent
      local humanoid = npc:WaitForChild("Humanoid")
      local humanoidRootPart = npc:WaitForChild("HumanoidRootPart")
      -- Reference the Animate script
      local animateScript = npc:WaitForChild("Animate")
      -- Access the built-in walk animation
      local walkAnimationTrack = humanoid:LoadAnimation(animateScript.walk.WalkAnim)
      local function followPlayer(playerRootPart)
      -- Recalculate the path every 0.1 seconds
      while true do
      -- Create a path to the target
      local path = PathfindingService:CreatePath({
      AgentRadius = 2,
      AgentHeight = 1,
      AgentCanJump = true,
      AgentJumpHeight = 5,
      AgentMaxSlope = 45
      })
      -- Compute the path to the target
      path:ComputeAsync(humanoidRootPart.Position, playerRootPart.Position)
      -- Check if the path was successfully computed
      if path.Status == Enum.PathStatus.Success then
      local waypoints = path:GetWaypoints()
      -- Play the walk animation when the NPC starts moving
      walkAnimationTrack:Play()
      -- Move the NPC along the waypoints
      for _, waypoint in ipairs(waypoints) do
      -- Check if the NPC should jump at this waypoint
      if waypoint.Action == Enum.PathWaypointAction.Jump then
      -- Make the NPC jump
      humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
      end
      npc.Humanoid:MoveTo(waypoint.Position)
      --npc.Humanoid.MoveToFinished:Wait() -- uncomment to make NPC move to outdated positions
      if (humanoidRootPart.Position - playerRootPart.Position).Magnitude < 2 then
      -- Stop the walk animation when NPC reached the destination
      walkAnimationTrack:Stop()
      break
      end
      end
      end
      wait(0.5) -- Recalculates the path every 0.5 seconds
      end
      end
      -- Function to find the nearest player
      local function findNearestPlayer()
      local players = game.Players:GetPlayers()
      local nearestPlayer = nil
      local shortestDistance = math.huge
      -- Find the nearest player based on distance
      for _, player in ipairs(players) do
      local character = player.Character
      if character and character:WaitForChild("HumanoidRootPart") then
      local distance = (character.HumanoidRootPart.Position - humanoidRootPart.Position).Magnitude
      if distance < shortestDistance then
      shortestDistance = distance
      nearestPlayer = character
      end
      end
      end
      return nearestPlayer
      end
      -- Constantly chase the nearest player
      while true do
      local nearestPlayer = findNearestPlayer()
      if nearestPlayer then
      followPlayer(nearestPlayer:WaitForChild("HumanoidRootPart"))
      end
      wait(0.1) -- check again every 0.1 seconds for player updates
      end