What do you want to achieve? Keep it simple and clear!
To make a pathfinding AI constantly move towards the players new position.
What is the issue? Include screenshots / videos if possible!
Not sure how this is possiable to make this work without breaking the pathfinding system.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have recenlty got my AI to naviget towards a players position, but in order for the AI to keep following it has to reach the location of the where the player was last.
Is there a way to have the pathfinding service constantly update the postion of the player before it reaches the old position?
this is the code block I’ve figured out so far:
local function Capture(target)
local distance = (Root.Position - target.HumanoidRootPart.Position).Magnitude
local NewPath = Pathfinding:CreatePath({
["AgentHight"] = 4,
["AgentRadius"] = 5,
["AgentCanClimb"] = true,
["AgentCanJump"] = true,
Costs = {
Water = math.huge
}
})
NewPath:ComputeAsync(Root.Position, target.HumanoidRootPart.Position)
if distance > 5 then
if NewPath.Status == Enum.PathStatus.Success then
for index, target in pairs(NewPath:GetWaypoints()) do
Humanoid:MoveTo(target.Position)
Humanoid.MoveToFinished:Wait(0.01)
end
end
else
attack(target)
end
end
Update: I have managed to get the NPC to update to the players position, but it does struggle to find it’s way aound corners. and my attack function is a bit buggy. Does anyone know any way I can improve it anymore?
local function attack()
local Debounce = false
Entity.Humanoid.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not Debounce then
Debounce = true
Attack:Play()
wait (1)
hit.Parent.Humanoid.Health -=5
Attack:Stop()
Debounce = false
end
end)
end
local function Capture(target)
local distance = (Root.Position - target.HumanoidRootPart.Position).Magnitude
local counter = 0
RunSerivce.Heartbeat:Connect(function(step)
counter += step
if counter >= 1 then
counter = 0
local NewPath = Pathfinding:CreatePath({
["AgentHight"] = 4,
["AgentRadius"] = 5,
["AgentCanClimb"] = true,
["AgentCanJump"] = true,
Costs = {
Water = math.huge
}
})
NewPath:ComputeAsync(Root.Position, target.HumanoidRootPart.Position)
if distance > 5 then
if NewPath.Status == Enum.PathStatus.Success then
for index, target in pairs(NewPath:GetWaypoints()) do
if not Walk.IsPlaying then
Walk:Play()
end
Humanoid:MoveTo(target.Position)
Humanoid.MoveToFinished:Wait(0.01)
end
else
Humanoid:MoveTo(target.Position - (Root.CFrame.LookVector * 10))
end
else
if Walk.IsPlaying then
Walk:Stop()
end
attack(target)
wait(2)
end
end
end)
end
Also here is an example of the NPC having issues with corners: