An Alternative To Grid-Based Pathfinding


Most games use grid-based pathfinding to ensure that enemies, units, or whatever other agents the game has end up where they need to be. Grid-based systems are both precise and predictable, making them a superb choice for most games. We experimented with this approach in Mars Commander as well, but the results were less than satisfactory. Grid-based pathfinding simply didn't look or feel good in our game. 

By the way, for a quick intro to pathfinding as a concept, check out this link: https://www.codingame.com/learn/pathfinding

Units in Mars Commander are fast. Our scout unit tops the list with a max speed of 45 km/h, but our other units are no slouch either. Because of the low gravity environment, acceleration is blazingly fast as well, meaning our units are able to traverse most stretches of terrain in a snap. 

With such speeds, predictability of movement is a must, and that is precisely where the issue lies. Since our units hover in a semi-realistic fashion, we cannot predict their future location in the same way that many other strategy games can. We simply don’t know for sure at what exact location they will end up because too many factors are involved. 

In Unity, Rigidbody.AddForce is arguably the most realistic way to make GameObjects such as players, projectiles, or enemies move. Instead of specifying where an object will move to and with what speed, we simply add a force to it. That force is what will propel the GameObject in the desired direction. All our units use this method and this method alone, regardless of whether they are controlled by the player, allied AI, or enemies. 

Moreover, since our units hover along the y-axis they will often move in multiple directions at once, leading to even more complexity. Grid based pathfinding isn’t a workable approach here since it will often slow units down. Furthermore, the squarish nature of the grid itself will create sharp, unnatural looking movements that are completely out of sync with the game’s speedy, fluid nature. 

So what is the solution? In the case of Mars Commander, we opted for an obstacle detection system using raycasts. Raycasts are Unity’s most straightforward method for detecting obstacles along a certain axis or line. While not perfect, this method ensures that our units will only avoid or turn when they actually need to, not because they hit the edge of an arbitrary grid square.

The result is a fast, fluid system of movement that looks great, especially around the large, open terrains that form our levels. While we may opt for a slightly different system of obstacle detection in the future, we will never revert to a grid based system. 

In all honesty, the movement system laid out above has given us plenty of headaches, as it wasn’t exactly straightforward to implement. It also meant that we couldn’t use third party assets for pathfinding, as these simply weren’t compatible with what we had in mind. If that hadn’t been the case, we would have certainly checked out an awesome asset like A*: https://assetstore.unity.com/packages/tools/ai/a-pathfinding-project-pro-87744

Different systems of pathfinding have different pros and cons, but the above is what worked best for Mars Commander. What do you think of this system? And if you’re a fellow game dev, what system have you adopted in your game? Don’t hesitate to let us know in the comments!

Get Mars Commander

Leave a comment

Log in with itch.io to leave a comment.