Pathfinding in 2D platformers, or even platformers in general, is oftentimes very basic. Many 2D platformer enemies that move don’t even use advanced pathfinding techniques; many are programmed to simply move in a direction indefinitely, maybe towards the player, turn around when it hits a wall or ledge, or perhaps it’ll fall off that ledge instead.
If we don’t need the AI to follow specific rules of the game to navigate, then things get pretty easy. Say for example we need to have a bird or some flying enemy follow the player. One A* implementation later and it's working as intended. But in the event that the AI needs to navigate the level as intelligently as a player would have to, using similar techniques and interactivity with the environment, things get a little difficult.
Take my crudely drawn example image. The stickman is our AI, and he wants to reach the flag, but he has a problem in front of him. First, his pathfinding has to recognize the spring and the ladder, and what they can do to help him get to the flag. Then he needs to figure out which is the faster way to go; jumping on the spring, or climbing up the ladder. Implementing a solution that can recognize these things is not impossible by any means, but it would certainly be time consuming.
The alternative to making something that gives instructions to the AI procedurally is to make the instructions ourselves.
For my tech demo, I utilized the free A* Pathfinding Project extension for Unity to make a point graph. Then, I created a few pink enemies that used the A* algorithm to find the best path to the blue player. After that, I made a custom velocity-based movement script that moved the pink AI enemies towards its next node horizontally, while jumping if the next node is above it. So far, my solution is pretty basic, but the most important part of this graph is the custom behavior nodes. The custom behavior nodes can be passed a behavior that takes the enemy AI game object, and performs an action to it. This could be anything from performing a big jump on the spring in the middle of the scene, climbing up a ladder, pushing a button to open a door then walking through it, etc. The strength of this design is in its modular nature; it can be used for any action one could think of.
Some games this kind of solution could work for:
Games that require AI that emulates a player’s actions
Games with a small amount of carefully designed levels
Games where complete control over AI pathfinding is desirable
Some potential cons to this approach are:
It could give too much extra work to the designer to make detailed point graphs for all the levels
In the event that user generated content becomes a thing later down the line, you’d need to create something procedural so those levels work
Too many levels to make detailed graphs for would be a huge time sink
When I was researching solutions to this problem, I found a blog post from Joost van Dongen, a programmer for the game Awesomenauts, a 2D platformer MOBA style game. Their solution involved creating both a point graph as well as a behavior tree. This article was what made me decide to use a point graph to create my tech demo. I also found a Gamasutra article by Yoann Pignole on creating a procedural 2D platformer pathfinding solution. The article was very informative, but most notably, it was very dense with explanations on calculating jump width to create node connections, and defining what was walkable ground and what wasn’t. The complexity of this solution is what further drove me into creating something that was simpler to implement with comparable results. The A* Pathfinding Project by Aron Granberg was also a huge help in the creation of the solution by providing a point graph system and pathfinding for the enemies.
Conclusion:
Through this project, I learned a lot about how many complexities come with creating intelligent pathfinding AI. Throughout my experience with learning Game AI solutions, they have been in controlled environments outside of real games, which makes me excited to learn more by implementing them in future personal projects.
Please email me at daniel.hman123@gmail.com with any questions.
References:
Kommentare