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

Why does my Pathfinding NPC not move Smoothly?

Asked by 4 years ago
Edited 4 years ago

I'm trying to make an npc that follows the closest player. I have everything working (to my knowledge...) however the npc is a bit robotic in that it does not have smooth transitions between waypoints. I'm trying to figure out why that is. (i've ruled out lag since there are other npc's that move smoothly)

Here is a gif of whats happening https://gyazo.com/dbce5cbb8e78ab6a8b38c799dd77a68b

Since I'm not sure exactly what the problem is, here is the full script

local zombie = workspace.Zombie
local humanoid = zombie.Humanoid
local pathFindingService = game:GetService("PathfindingService")
local wayPointsIndex
local maxDistance = 200--detection range
local wayPoints = {}
local currentPoint

function findtarget()
    local closestDistance
    local closestPlayer
    for i,v in pairs(game.Players:GetPlayers())do
        if v.Character then
            local distance = (v.Character.HumanoidRootPart.Position-zombie.Torso.Position).magnitude
            if not closestDistance then closestDistance = distance end 
            if distance <= closestDistance and distance < maxDistance then
                closestDistance = distance
                closestPlayer = v
            end
        end
    end
    return closestPlayer
end

function createPath()
    local player = findtarget()
    if player then
        local character = player.Character
        local path = pathFindingService:CreatePath()
        path:ComputeAsync(zombie.Torso.Position,character.HumanoidRootPart.Position)
        local wayPoints = path:GetWaypoints()
        if path.Status == Enum.PathStatus.Success then
            for i,v in pairs(wayPoints)do
                if v.Action == Enum.PathWaypointAction.Jump then
                    humanoid.Jump = true
                end
                humanoid:MoveTo(v.Position)
                humanoid.MoveToFinished:Wait(2)
            end
        end
    end
end


while true do
    createPath()
    wait()
end

Any help is appreciated :)

0
I had the same problem. but I noticed in a server npc pathing movement is smoother compared to in the studio. mantorok4866 201 — 4y

1 answer

Log in to vote
0
Answered by
Yuuwa0519 197
4 years ago

I used to have the same problem too. I am not certain if this is the same problem as I encountered, but the way I solved about robot-like movement is by setting the network ownership of the npc to server(to nil), which will prevent from the server to automatically assign the client to calculate the physics of npc when they are close, which causes the npc to lag.

local function itterateModel(obj)
    for I,v in pairs(obj:GetChildren()) do
        if v:IsA("BasePart") then
            v:SetNetworkOwner(nil)
        end
        itterateModel(v)
    end
end

local npc = script.Parent

itterateModel(npc)
Ad

Answer this question