Moving NPCs Between Points

Moving NPCs Between Points

10 min

NPCs, or non-player characters, can add a lot of depth to a game. NPCs can be enemies for players to fight, colorful characters that players can talk to and interact with, and more.

Sometimes an NPC can stand in one place, like a shopkeeper working at a small in-game shop. In other cases, an NPC will look much less robotic (and more human!) if it moves around, even if it’s basic movement between two or more points.

Moving to a Point

A simple way to move an NPC is with the Humanoid object, a special object that gives a Model|model the functionality of a character, even if it doesn’t look human. This allows the model to physically move around the place and interact with objects in it.

Consider this zombie. Zombies aren’t very smart, so we can make it walk in a straight line without worrying if anything is in the way.

For convenience, the green flag model inside the game has been named GreenFlag. We’ll use it as the first destination for the zombie to walk to. Consider this example:

In this basic code, we find the Zombie model in the workspace, get its Humanoid object, set the green flag as the destination point, and then tell the zombie to walk toward it using the Humanoid/MoveTo|MoveTo() method.

Moving to Additional Points

Moving an NPC to one point is cool, but what about moving it in a series of points? You could script a bunch of Humanoid/MoveTo|MoveTo() and wait() commands in a sequence, but that’s tedious to code.

A better solution is to use the Humanoid/MoveToFinished|MoveToFinished event. This event lets you pause a script until the Humanoid/MoveTo|MoveTo() action is completed and the NPC reaches its destination. Then you can continue running the script, moving the NPC onward to another point or back to its original point.

In the following code, we create another variable, pointB, for the purple flag. Immediately after sending the zombie toward the green flag, we pause the script. Once it reaches that flag, the script continues and we send the zombie moving toward the purple flag.

Cycling Between Points

Now that the zombie moves between the two flags, let’s finish up this script by adding:

  1. A loop which makes the zombie continually patrol back and forth between the flags.
  2. A customizable “patrol delay” which makes the zombie pause for a short time before moving to the next point.

That’s it! Now the zombie patrols between the flags, stopping to rest for 2 seconds each time.

Where next? As you can see, straight-line movement between points is simple but it’s also limiting. If any obstacle exists on the path (wall, river, cliff, etc.), the NPC might never reach its destination. Zombies don’t have big brains, but other NPCs should act “smarter” and try to find the best path to a destination. Learn how in the Articles/Pathfinding|Pathfinding guide!

  • npc
  • path