#Pathfinding issue

1 messages · Page 1 of 1 (latest)

still pasture
#

Hey, it's been a while I haven't coded in Luau, and I've been trying to do some pathfinding.
Do someone know why is my pathfinding always late?

#

I did my best to optimize it so it's probably not because of that, I think I forgot something or just did a stupid mistake.
There is no task.wait(x) with x >= .01 , so I don't know why it's so slow

#

There's a lot of code so I can't rly send everything, but the main code is like this:

function Zwerg:Chase()
    self.model.Humanoid.WalkSpeed = self.ChaseSpeed
    self.chaseAnim:Play()
    print("start chasing")
    
    while self.Target and (os.clock() - self.Target[2] < self.ChaseTime) do

        local plrChar = self.Target[1].Character or self.Target[1].CharacterAdded:Wait()

        local path = Path.new(self.model.PrimaryPart, plrChar.PrimaryPart)
        path:Compute()

        if path.Status == 0 then
            path:Show()
            local initialDestPos = plrChar.PrimaryPart.Position

            for _, waypoint in path:GetWaypoints() do
                self.model.Humanoid:MoveTo(waypoint.Position)²
                task.wait()
                --self.model.Humanoid.MoveToFinished:Wait()
                

                if not self.Target then
                    path:Free()
                    self.chaseAnim:Stop()
                    return true
                end

                if (plrChar.PrimaryPart.Position - initialDestPos).Magnitude > 5 then
                    break
                end
            end
        else
            print("no path")
            task.wait()
        end

        path:Free()
    end

    self.Target = nil
    self.State = "Idle"
    self.chaseAnim:Stop()
    return true
end
#

And the pathfinding looks like this

function AStarAlgorithm.ComputePath(origin, dest)
    local padding = 20
    local botLeft = Vector3.new(math.min(origin.Position.X, dest.Position.X) - padding, 0, math.min(origin.Position.Z, dest.Position.Z) - padding)
    local topRight = Vector3.new(math.max(origin.Position.X, dest.Position.X) + padding, 0, math.max(origin.Position.Z, dest.Position.Z) + padding)

    local ignoreList = {partFolder, origin.Parent, dest.Parent}
    if workspace:FindFirstChild("Npcs") then
        table.insert(ignoreList, workspace.Npcs)
    end

    local graph = createGraph(botLeft, topRight)
    local projectedGraph = GraphProjection(graph, ignoreList)
    local startNode = getClosestNode(projectedGraph, origin.Position)
    local targetNode = getClosestNode(projectedGraph, dest.Position)

    local rawPath = AStar(projectedGraph, startNode, targetNode, ignoreList)

    if not rawPath then
        for _, row in graph do
            for _, node in row do
                partCache:ReturnPart(node.part)
            end
        end
        return nil
    end

    local finalPath = rawPath --StringPulling(rawPath)

    for _, node in finalPath do
        node.onFinalPath = true
    end

    for _, row in graph do
        for _, node in row do
            if not node.onFinalPath then
                partCache:ReturnPart(node.part)
            end
        end
    end

    return finalPath
end

function AStarAlgorithm.ReturnNode(node)
    partCache:ReturnPart(node.part)
end
#

I don't think this portion of code is enough to find the problem, and I'll eventually find it myself, but if someone ever had this problem they can probably tell me!

manic dagger
#

the server see you with some latency

#

For example, use their MoveDirection and add a small offset based on estimated latency