Simplest "Pathfinding" / Player Follow / Enemy Chase AI

This is one of the most basic ways to make Enemies follow your Player (only after seeing the Player).
I realized how complicated many of my pathfinding examples are, and I wanted to make a simplified one where (hopefully) more people can actually understand how the code works.
If you look in the code, could you please let me know if you understand it or not?

The basic idea is that the Player keeps adding their X and Y coordinates to the front of 2 lists, which get shared with the Enemies. The Enemies check if they can see the 1st position on the list, and if not, then they check the 2nd, and if they can’t see that one, then they check the 3rd position, etc. until they find a past Player position that they can see, and then they aim for it. This is essentially the “Player Trail” concept but without an extra “Trail” object.
Again, please have a look in the code, and let me know if you understand it (or if there’s something I can do to make it more readable). Feel free to ask questions, too.

And finally, like most pathfinding examples I’ve made, this one can cause performance issues if the player’s trail is too long :laughing:

8 Likes

Very cool mate it seem very cool.

1 Like

Cool! The move around obstacles like walls!

1 Like

Yeah, lol, that’s what pathfinding is

2 Likes

It’s still cool.

Interesting.

Amazing. I always thought pathfinding would be a system that would take a bunch of objects.

2 Likes

So this technically isn’t typical “pathfinding”, it’s more like path following, but you could sort of call it “pathfinding” just by association.

2 Likes

Anyone feel like they understand this? I can explain it more if anyone’s interested (I’d really love for more people in the community to understand how a basic player follow works, so it’s not just me and a handful of others)…

3 Likes

I’m pretty sure I understand how this system works. You explained it pretty well in the first post.

2 Likes

I’m the worst representative of the community, but makes sense to me lol

2 Likes

flower
image

4 Likes

Almost forgot, I made a version of this where the enemies don’t need to rotate (so you can still animate them as normal sprites, etc.):

If you use this, just try and make sure that the enemies and walls have 0 friction, otherwise they could get stuck.