how can i make multiple npc follow each other in a row?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Laftar

I’m trying to get a player to save the npc, and I wanted them to follow the player in sequence (the npc follows whatever is in front of it). Initially, I’m using Astar 2D to trace the path through the center of the tilemap. at first it works, but it starts to have some problems when I go around the bodies, where one tries to go over the other. I would like them to walk in a line, without stepping over each other. similar to a “Big Snake”, does anyone have any idea how to do this?

Can’t they reuse players pathfinding ? I mean they could use the same path, but its last point would be determined by their queue.

Inces | 2022-10-14 12:58

:bust_in_silhouette: Reply From: alexp

You’re going to have to decide on a way for them to sort themselves so that each is following the next, i.e. if they join the group at different times, then keep track of the last member, and designate the new member to follow them. Then you can use your path finding as before, and just regularly update each NPC’s target position to be behind whoever they’re supposed to follow. The position “behind” an entity can be determined by getting a vector for the direction they’re facing and reversing it, and then multiplying that vector by your desired follow distance.