NPC maintaining it's distance from the player? (Undesired effect)

I’m working on an NPC zombie system which essentially should catch up with the players and do a certain amount of damage. However no matter what speed the zombie is walking at, it will always maintain a certain distance from the player. Is there a way to possibly make the NPC catch up to the player?

Also I think it’s important to mention that it only maintains the distance when the player is in motion.

https://gyazo.com/6516036d16e7cc60bcd031404e4e52a6

Can you explain how you are making the NPC move?

I’m using PathfindingService in order to compute the path between the zombie and the nearest player. Basically making it move from one waypoint to another.

I can help you with this. I used pathfinding service with my train game too. Just detect the distance between zombie and the target ( player ) and if its under 1 it will cause damage to the character.

while wait(0.1) do
for _, waypoint in pairs(waypoints) do
-- counts how many studs btween the start and the end
-- Use while wait(0.1) do to check every 0.1 seconds.
-- If the returned value is 1 then use a script to damage the player
studsleft = studsleft + 1
end
if studsleft <= 1 then
-- cause damage to the player
end
end

What’s happening here is this:

  • zombie computes a path to the player consisting of nodes
  • by the time the zombie reaches the last node, the player has moved! now the zombie must stop and recalculate.

So essentially the zombie isn’t making updated paths quick enough, so he lags behind.

To fix this, simply cast a ray at the player. If the ray hits the player, then there is nothing obstructing the path, and thus no pathfinding is needed. Simply move the zombie straight towards to player, without plathfinding.

You can think of it as two modes:

  • pathfinding mode: no direct path found, pathfinding needed
  • follow mode: no obstruction, move straight to target without pathfinding\

This was the way I semi-solved the laging-behind problem with one of my npcs, but I never fully fixed a strange error with the npc perpetually switching modes, giving the path a very twitching and undecisive look. Probably do to poor coding :stuck_out_tongue:

4 Likes

The best option I can think of, is raycasting to see if there is a straight line between the AI and the target. If there is a straight line, simply move the AI to the target. But, if there is not a straight line, use the FindPathAsync to find a path directly towards the target.

1 Like

Would that be efficient if I were to run it with while wait() loop?

Ideally you don’t want to be re-calculating your path 30 times a second. You can read up more on why that’s bad here

to run this to the best of the ability, you could run the update loop about every 0.5 seconds (even something like 0.7 or 1 would be fine).

doing this, your zombie won’t be catching up to players while in pathfinding mode. The pathfinding is only to get you close enough to the player so that you got a straight shot (follow mode).

1 Like

I figured I could calculate the distance between the zombie and the player. If it’s less than 5 then just turn off the pathfinding and move the humanoid straight to the player. I think this method is less costly

This does not account for walls. If a wall is in the way, the zombie will try to walk right throught it.

But in all seriousness I don’t think there’s a big chance of something getting between them (keep in mind they will be only 5 studs away from each other) .

Do a wall check using raycasting.