#Tower Defense Enemy Movement System Glitchy

22 messages · Page 1 of 1 (latest)

mossy plank
#

For some reason the current one I'm working on is slightly bugged and it has these position stutters: Here's what it looks like:

#

here's how they start
here's how they look after they crossed several nodes

#

their distance between each other seems to break each time they turn

#

Well, you get the idea.
Essentially, here's the code for moving them:

#
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
    for _, npc in ipairs(game.Workspace.TowerDefense.Test.Mobs:GetChildren()) do
        local distance = (game.Workspace.TowerDefense.Test.Path[npc.Settings.GoalNode.Value].Position - npc:GetPivot().Position).Magnitude
        if distance < 0.5 then -- original 0.5
            if npc.Settings.GoalNode.Value ~= #game.Workspace.TowerDefense.Test.Path:GetChildren() then
                npc.Settings.CurrentNode.Value = npc.Settings.CurrentNode.Value + 1
                npc.Settings.GoalNode.Value = npc.Settings.GoalNode.Value + 1
                npc_library.Rotate(npc)
                npc:PivotTo(CFrame.new(game.Workspace.TowerDefense.Test.Path[npc.Settings.CurrentNode.Value].Position, game.Workspace.TowerDefense.Test.Path[npc.Settings.GoalNode.Value].Position))    
            else
                print("DONE!")
                npc:Destroy()
            end 
        else
            npc:PivotTo(npc:GetPivot() + npc:GetPivot().LookVector * 20 * deltaTime) -- original 20
        end
    end
end)
#

What I'm trying to achieve is to space them all out evenly exactly how they spawn
but some reason they end up catching up to each other although they are all at the same speed

mossy plank
#

still need help

mossy plank
#

still need help

mossy plank
#

still need help

mossy plank
#

still need help

silver crescent
mossy plank
# silver crescent can't understand a single block of code, simplify this up.
local Mobs = game.Workspace.TowerDefense.Test
local Path = game.Workspace.TowerDefense.Test.Path

game:GetService("RunService").Heartbeat:Connect(function(dt)
    for _, npc in ipairs(Mobs:GetChildren()) do
        local Set = npc.Settings
        local CurrentNode, GoalNode = Set.CurrentNode, Set.CurrentNode
        local distance = (Path[GoalNode.Value].Position - npc:GetPivot().Position).Magnitude
        
        if distance < 0.5 then
            if GoalNode.Value ~= #Path:GetChildren() then
                CurrentNode.Value += 1
                GoalNode.Value += 1
                npc_library.Rotate(npc)
                npc:PivotTo(CFrame.new(Path[CurrentNode.Value].Position, Path[GoalNode.Value].Position))    
            else
                print("DONE!")
                npc:Destroy()
            end 
        else
            npc:PivotTo(npc:GetPivot() + npc:GetPivot().LookVector * 20 * dt) -- original 20
        end
    end
end)
#

simplified enough?

silver crescent
#

The issue is the server ping

#

You need to optimize it so that it can run smoothly on the server

#

Or could be something that is blocking the path

#

and then delay it

#

My thought on this is the server ping, happens to me alot too.

mossy plank
#

alright ill look into it