How to make Humaoid move along specific path?

So I’m trying to make a tower defense game and I encountered a problem. I don’t know what’s the best way to make an NPC walk along a specific path. I could use PathfindingService but then I would have to run that for each and every enemy that spawns in and that could be an optimization issue. I could also use humanoid:MoveTo() but if I wanted to have a curved path that could create a problem with needing to make multiple points for a curve. Is there a better way to do this? Thanks!

3 Likes

you could make a table of vector 3 positions that then can be moved to.

While I don’t understand them myself, you could use PathFindingService with Bezier Curves.

Yeah but if I wanted to add curves to the path that would get very tedious to do.

1 Like

I don’t think it would be possible to use PathfindingService with Bezier Curves. But that’s just based off of what I read on the article you included.

1 Like

these are actually able to be used in pathfinding service. image

Sorry I forgot to mention this previously but I forgot that I don’t want to use PathfindingService since it would have to calculate a path for each new enemy. Sorry!

hmmm.
try this:

targetPos = 1 --the number position.
local hum = script.Parent.Humanoid
local path = workspace["PathFolder"]:GetChildren()
function moveTo()
    local Position = nil
    for i,moving_to in pairs(path) do
         if moving_to.Name == tostring(targetPos) then
            Position = moving_to.Position
        end
        hum:MoveTo(Position)
    end
end

hum.MoveToFinished:Connect(function()
    targetPos += 1
    if targetPos < #path then
       moveTo()
    else
       print("Done moving!")
    end
end)

the path is basically a folder. it finds a part with a name like “1”
when it finds that a part with a name is equivalent to “tostring(targetPos”):
if part.Name == tostring(targetPos) --example here.
then it basically set the position to that part position where the humanoid will move
so basically: create a path folder and insert a part in the folder then name it like numbers in orders.
first path = 1
second path = 2
and so on…
EDIT: you should use collection service for controlling the zombie movements without actually creating too many script and just clone them

i beleive this post explain it better

If your map that the enemies are moving on doesn’t change much, and assuming that your enemies all spawn at the same place, then you maybe can use pathfinding. You’d not need to run it for each enemy, just run once at the start of a wave/level/whatever to calculate the path between the enemy spawn point and the end point, and that will give you a list of waypoints.
You can then use those points for all enemies, don’t need to recalculate a new path for each one.

If enemies spawn in different places you’d still just need to calculate paths for each spawn point, not each individual enemy.

2 Likes

One more problem, what if I wanted the enemies to walk in the center of the path? But so far this is looking like the answer! Thanks!

EDIT: I assume that the Pathfinding algorithm will try to find the shortest path like staying near the walls but if not then I assume that’s it!

I actually solved this by using a Bezier curve plugin and looping through the position of the parts but I would still like to know the answer to doing this dynamically!

1 Like

Sounds like its all fixed, but yes pathfinding will find the shortest path, so can try to cut corners.
If you want it to stay in the middle of the path you can look at increasing the agent size so that it’s just a little smaller than your path width. That should force it to stay pretty much in the middle.

That actually sounds like it could work but how could I make the NPC stay on the path without creating walls around the path? Thanks for the help so far!

In my opinion, i think that you don’t need to use pathfinding because all the path are near each other so you might not need to use a pathfinding, instead use a :MoveTo() to the nearest path!

NOTE: im new here so my explanation might be bad

1 Like

that honestly could work; i might try reworking my script so it will work with different paths; though i think the answer would be using math functions called math.rad?

You should find a plugin that can make a curvy path Burt I don’t really know what is the plugin called :slight_smile:

An idea might be to have nodes throughout the path, storing them in order in a table, and then using MoveTo() to move to each node

If its a path finding npc use:
Pathfinding Service

But if its a story game and you want the npc to go to a place in a specific route, you can do this:

local routespawns = {} -- all the vector3 positions

for i = 1,#routespawns do
   local npc = npc -- your npc
   npc.Humanoid:MoveTo(routespawns[i])
end

So your npc moves to the route, and sometimes the npc gets stuck when using custom path, you can also use path finding service to move from each position to other position

Its clear, as the other posts in this thread have illustrated, there are many options to move NPCs from one point to another, including a series of movements along a path. However, there are two factors to consider when choosing that have not been mentioned much in this thread. I feel that threads too often skip over discussing considerations of one option over another and assume everyone reading knows what to consider already.

  1. What is your desired movement behavior of your NPCs?
  2. How much can your game afford to spend on computing movement without negatively impacting player experience such as lag?

For the first question,

1.1 Will your levels including parts/obstacles that dynamically change that could impede/block the path of the NPCs?

  • MoveTo, body movers, and tween service will move a NPC from one point to another without consideration of what is between the two points, which could result in your NPC getting stuck and continuously running into a wall until the movement is ended. Pathfinding can be used to make adjustments along the path to move around obstacles, where a path can be found to still reach the destination. Maybe you can attempt to build in some adjustment logic to your non pathfinding option, but then again do you feel you could create a more efficient “pathfinding like” method?

1.2 Are you OK with dumb NPCs?

  • Maybe your OK with your NPCs getting stuck behind obstacles and a less resource intensive movement method is a better option.

1.3 Do your NPCs need to change their paths at anypoint during the gameplay such as to chase a player if they get too close?

  • If your NPCs need to change their movement on the fly simply iterating over a predetermined path may not work, at least without some additional logic to determine when to break out of that path and when/how to re engage that path. Perhaps, you would need iterate over the pre-determined path to find the closest point to know where to re-engage the path. Otherwise, pathfinding may be able to do this with less work on your end as it would determine a new path to reach the destination from where ever the NPC is at that point in time.

As for the second question I am no expert. I’ve read a lot of different opinions on optimization of NPC movements, but have not found anything that is clear the most optimal strategy is. The consensus across what I have read suggests pathfinding is more resource expensive than approaches like tween service or raycasting to calculate movements on the fly, but these have their limitations and require more custom logic. You may just need to experiment to determine whether the best option for your game is. Though a few factors to consider may be

  • How many NPCs and players are you expecting to have any any given point? As the number goes up the more risk of lag with more expensive movement methods
  • How large, complex, and dynamically changing are your levels? The bigger the level, the more complex, and the more dynamic the level the more frequently your NPCs may need to recalculate their movements and the higher the risk of getting NPCs suck.

The reason I need this is for a tower defense game I’m making, so I really only need the path to be calculated once then applied to all NPC’s. There might be steps, curves, different angled turns, and maybe other stuff.