How do I make My NPC follow the player in a single file line like in undertale and in deltarune?

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

How do I make My NPC follow the player in a single file line like in undertale and in deltarune?

Im trying to make the NPCS Follow the player in a line i am using a standered moving script for my player movement but how do I make the NPC’s follow behind the player?

:bust_in_silhouette: Reply From: Nickyroo

I have never personally scripted an NPC like this, but what I would do is to create a short array that saves you’re main players position every X seconds, Basically saving a Trail of breadcrumbs

and then I would have the NPC move to the Most latest point on the array… once that point is reached (or close enough), erase that array point that the Character Reached… and have the Character continue to follow the next latest array points… Like Checking off a Checklist oldest first

And finally, so the guy does not just invade the main players space, I would do a distance check… and just clear the trail array… Why I would prefer to clear your array when a distance is Reached, Is because I imagine that if you were to walk downwards, and then immediately upwards, You would have an Npc just phase through you to get behind you (Unless that is what you want)

This is simular to a Character following an A* path at a basic level… Incase you dont know what A* is, its basically a name for a pathfinding Algorithm…

If you use A* it would allow for the Characters to make their own Decisions/make their owns paths and such to reach your player! if you are interested in researching about it

:bust_in_silhouette: Reply From: stormreaver

There are (as usual) many ways to accomplish this. I do something similar (but different) in my game for collectible items that must fly from their current position to a player.

I create a new instance of a movement node (it’s a Spatial subclass) that is attached to each moving collectible that moves the collectible towards a destination node. The movement node has a reference to the target object so it can always know the target’s global transform.

On each frame, it recalculates a straight line between where it is and where it needs to be. It then moves the collectible an incremental distance in that direction. This can be the basis of what you need.

After you have the movement node done, all you would have to do is calculate the point behind the player that the movement node is targeting, and pass that to the movement node as the target.