Looking for a way to move a NPC to a moving object

I am trying to make a NPC move toward a player so that the NPC can be used as a bot in my PVP game while the player is playing single player. My problem is that, when using Humanoid:MoveTo() the NPC walks off the map. As you can see, that is not an option on my map…

when using PathfindingService, the NPC does not walk off the map, however they walk to the position of the player when the pathfinding was called. this means if player is at Point A when the pathfinding is called, and then player moves to Point B, the NPC will still move to Point A and the player can easily dodge them. I am looking for a way to combine the best of both, where the NPC does not walk off, but also does not feel delayed and can update real time. Does anyone know how I can do this? Thanks in advance!

1 Like

You can use collision service on an invisible barrier around the walls so that it only collided with bots. I can’t explain how to use it right now but you can easily google it and watch/read a resource there.

1 Like

yes, but then the bot will just push against the barrier forever when trying to walk toward the player. the bot also should be able to fall off, (I am sure I can turn off the barrier when the bot is hit) so that would be a problem. the main issue would be how the bot just would endlessly try and walk toward the barrier though.

You can use pathfinding with raycasts. First check if a ray can go from the npc to player. If it can, then make the npc :MoveTo the player. If the ray doesn’t hit, then use pathfinding service with the invisible map borders.

In that case, try checking when the players position changes, then call the :MoveTo function again. I’m sure this isn’t the most efficient way but I think it will work.

Well you can constantly update the path. You could also make it so when they’re close enough to the player it’ll just move directly towards them (or follow the path they took). You can just raycast down and make sure it doesn’t walk off the edge.

But another thing to consider is how simple your map is. I mean right now your map is a fairly simple shape, but I guess it’s liable to change though. But with a shape that simple it wouldn’t be hard to just let it know where the bridges are when it needs to cross. The shapes of your islands are almost convex, so you could just tell it to go to any point on the island and so long as it’s on the same island it shouldn’t really fall. Of course it’s not as simple as this if you add obstacles, or make the islands much bigger. But a custom pathfinding solution might be worth it.

Keep in mind there is still going to be the time between client and server communication. So you’ll either have to project where the client is going on the server, or you’ll have to run this on the client to ensure that the AI is actually capable of catching the player.

2 Likes

won’t the ray hit them though, as there are no walls?

I like the custom pathfinding idea, however I have never made a system like that before. Do you just use :MoveTo() combined with some logic so the bots move toward the bridges when the player is on a different island then them? Im sure I could use Region3s to mark the bridges, islands, etc. then use this data to tell when the bot needs to cross a bridge to reach the player.

Just create invisible walls for the ray to collide with. Only move the npc if the ray hits the player

1 Like

Yes. That’s pretty much exactly it.

When a bot is on the same island as a player, it simply needs to walk towards them. This is because your island shapes are nearly convex (they aren’t, but it’s close enough there shouldn’t be too many issues). Basically a convex shape means that you can draw any line from any direction through the shape and have it hit the walls at most twice. Since any line should stay within the shape as long as both points originate from the shape that means you can walk straight to the player.

If a player is on a different island. You simply need to figure out which bridges need to be crossed to get to that island. This is the pathfinding algorithm. If you want to look into well known pathfinding algorithms, you can look into a*. There are a lot of resources that will help you implement it if you look. But it wouldn’t be too difficult to just come up with one yourself. With such a small map you could even just cache the answers in a lookup table. If you intend larger though, an actual algorithm would be better.

Then once you get which bridges you need to take (and direction) you just tell the bot to go to a marked point near the entrance of the bridge on the island they are on. Then if your bridges are perfectly straight you can tell the bot just to go directly to the point on the other side of the bridge (otherwise you’ll have to have a few points for it to traverse). Just repeat this bridge crossing until you are on the same island as your target and then you can once again move directly towards them.

You will need a way to tell when the player is on a bridge as well and what to do in that case. So don’t forget that.

Basically the map would look something like this (or however you decide to structure it)

local island = {
    game.Workspace.Island1,
    game.Workspace.Island2,
    game.Workspace.Island3,
}

local bridges = {
    {2,3},
    {3,1},
    {1,2},
} 
--[[

The nested list above this might be a tad confusing at first glance.  It just shows which islands are connected.
If we were talking about island 1, then we would use the index 1 to reference the table.

So we would go islands[1] to get the island
And to get what other islands it connects to we go bridges[1].
That shows that island one has a bridge to island 2 and to island 3.
]]

I probably made this seem more complicated than this actually is. So of course don’t feel that this is your only solution, it can be a bit more involved than other solutions. I tend to use parts with object values to build the connections which my code interprets meaning once it’s programmed I don’t have to think about it again, but that in itself can be a bit tricky to set up initially.

1 Like

You could also just switch to roblox pathfinding if the character isn’t on the island you’re currently on if you want to set this up the easiest way you can. That way you don’t have to actually worry about implementing a datastructure to represent your islands or the pathfinding algorithm.

thank you! I just have 2 questions. One, my map will eventually have small props such as trees, rocks, etc. I am wondering how I can make the bot avoid these? For my second question, I am wondering what I should do if I make multiple maps. Should I mark them all by hand again, or should I spend longer making an algorithm so that the bots automatically pathfind? Thank you again for the help!

That is a bit trickier. If there are only a few props, you could just do a small raycast in the direction your bot is going and if it hits a prop just have it go around the prop. That would really only work with sparse props though. You might need a more robust system if you have a ton of props.

And as for generating them by hand or algorithmically. Really it depends. If it’s easy to do by algorithm, then by all means go that way, but if it’s far easier and less time consuming to just mark the maps by hand then that would be the approach I’d take. I usually hand mark my maps unless I’m generating them because it’s generally easy to add pathfinding data to my generation, but not always.

1 Like

thank you again! I will try and figure out how to do the anti collision aspect, but you have helped a ton with everything else!

1 Like