Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to combine pathfinding with MoveTo it get a fluid NPC that follows a player?

Asked by 4 years ago

I've been trying to make a zombie, and I'm fairly new to developing on Roblox. I tried using the code Roblox has for pathfinding, but The zombie would go where the player was previously and it was very easy to dodge the zombie. I've also made a script that only goes to the player's position with MoveTo.

wait(1)
fdistance = 100 --how close you want them to be before following
zombie = script['Parent']

while wait() do
    local closestp = nil;
    local closestdist = fdistance;

    for i,v in pairs (game['Players']:GetChildren()) do
        if v ~= nil  then      
            if (v.Character.HumanoidRootPart.Position-zombie.Torso.Position).magnitude <= closestdist then
                closestp = v
                closestdist = (v.Character.HumanoidRootPart.Position-zombie.Torso.Position).magnitude
             end
        else
            wait(1)    
    end
        if closestp ~= nil then
                zombie.Humanoid:MoveTo(closestp.Character.HumanoidRootPart.Position, closestp.Character.HumanoidRootPart)
        end
    end
end

This is what I'm currently using. It works well for open areas. but if the player walks behind something the zombie has no way of knowing how to get around it. How can I switch between pathfinding and MoveTo in a way the will make the zombie fluently chase a player?

0
If you use pathfinding service, your zombie has to regenerate its path periodically if the target player is moving. EmilyBendsSpace 1025 — 4y

Answer this question