XaiJu
taxiderby
taxiderby

patreon


Dev Tips: Path System

Note: The Senza Dev Companion Pack has been updated to go along with this tutorial, available to all patrons. The guide below focuses more on logic/methods than code, though, so it should be just as useful for those using GameMaker and the like!

Ranging from cutscenes, to puzzles, to even simple NPC behavior, getting a path system working in your game is PROBABLY a good idea. And since the cutscene system won the first poll, it was important to start with this first. For this guide, we're going to assume your character can already move at all.

First and foremost, you want to apply a list of way points (in this case, Vector2 positions) listed in the order you want the character to move to each. Where this list comes from can from any series of things, such as a basic path, or a dynamic path created by an object or other character.

Next, we get the character's normal move speed. Of course, if you're familiar with delta time, or how frames work, you know a character won't move three tiles in a single frame unless they're moving ridiculously fast or you have a frame rate of 1.5 per second, but it's lengthened to better explain it.

We compare this distance-per-frame (green) to the player's distance to the next node (yellow). If the move distance is shorter...

...we move the character to the next node by that amount (I personally use Vector2.MoveTowards to accomplish this), and finish for this frame. If needed, you can get the character's direction for their current position to the next node (nextNode - characterPosition) to determine which direction you want them facing.

However, if the player's movement distance is LONGER than (or equal to) the distance to the next node, we need to do three things:

First, we subtract that distance value from our movement value. Second, we move the player to the next node's position. Third, we remove the node for our list of way points. As a result, the node that was after it becomes the new next node.

If the character reached the final node here, we'd stop. But since it isn't...

 ...we repeat the process with the new next node, using our remaining distance value, all on the same frame. 

Repeat this process every frame until you get to the end!

Of course, this isn't limited to simply making the character follow a per-determined path.

Once More uses a similar method for companion following behavior — As the player runs, they create a path dynamically, which the companion character follows. If there's no path left, they move directly towards the player instead, stopping within a certain distance.

Puro utilizes a similar system for its twisting path, using a faux x coordinate and placing the player, enemies, etc along the appropriate part of said path, giving it its psuedo-on-rails gameplay style.

It can even be created by the character itself, but utilized in reverse if you want a "reverse time" effect — recording their positions and playing them backwards.

If anything is unclear or you feel needs further explanation, let me know! A patron-exclusive tutorial will follow very soon, discussing the path-editing tool I developed for Once More!

Dev Tips: Path System

More Creators