Week 5: Over-engineering enemy pathfinding

EarlyOwl
4 min readSep 18, 2021

Hello Everyone. Welcome to the 5th week! (Gameplay Sprint — for those who are counting.) Here’s an update on what I was working on last week.

TLDR:

  1. Created a hexagonal tilemap for enemies to travel around in a predefined path.
  2. Over-engineered a solution that was difficult to work with.
  3. I ditched the previous solution and tried to create a more manageable one using a simple waypoint system.
  4. Still, it was tedious to assign the tiles one by one.
  5. Discovered “Handles” in Unity.
  6. Used it to create a system that defined paths for the AI enemies conveniently.
  7. Then I lived happily ever after!

For more elaboration on that, read on.

I have a particular idea for the combat in the game. And to avoid spoilers, I’m going to keep it a little hush-hush for now. But I can tell you this much — the combat requires to happen on a bunch of hex tiles. I wanted the enemies to travel on it in predefined paths. (This can get randomly generated, but I haven’t decided on that yet.)

My first approach to pathfinding

My initial approach was to make individual tiles self-manage the direction in which the enemies entered and left. They can enter from any edge, but they will have to reach the center point of the tile. Then it will intelligently choose between the available path and exit the tile. All I had to do next was create chains of these tiles, and voila — it would work flawlessly.

I created a hex tile with six gates on its six edges and a central hub that decided the direction of the enemies’ exit.

And it worked. Of course, there was a problem that if the path looped, there was a chance it would go back the way it came. But I can avoid that by making the track one way and avoiding loops. Even if we had to have a loop, I could develop a solution to prevent such traversal of enemies.

Great! What was the problem?

Well… it was stupidly hard to define a new path. It meant that when I wanted to make changes to the track to test something, it would take ages to create new variations.

I was also beginning to feel that I had over-engineered the solution. I even searched the unity asset store to look for some plugins for easy pathfinding implementation. Yet, I decided against it as I didn’t want to bloat the game with unnecessary stuff.

Nevertheless, it was painfully clear — this wasn’t going to fly.

Next solution: Waypoints

I have worked with waypoints in my earlier projects, but I thought my previous solution was better for a couple of reasons. In the earlier iteration, the tiles didn’t require knowledge about the adjacent tiles and what they were up to. Each tile was independent and could operate on its own. But for the new solution, the tiles had to be aware of their neighbors, a bit at least.

Each tile will have information about the next tile it’s connected to. That way, I can create a chain of hex tiles to make the final path. And the tiles didn’t have to be complicated like the previous one.

This simplified lot of things, and it worked excellently.

But the problem of editing the path still loomed. Each time I wanted to change the track, I’d have to set the individual tiles one by one again. But this time, I approached with a plan in mind. It was a bit hacky, but I figured it could work.

Solution — another layer of waypoints on top of the tiles.

This time I created the waypoints with simple cubes which indicated the corners of the path. Each cube had information about the next cube in the line. When I start the game, it takes the first two seconds to travel across the line, updating the information for the hex tiles underneath. Once done, the path was ready, and the cubes vanished.

It was brilliant, but it meant visualizing the path in the edit mode would be difficult. I’ll be only seeing a bunch of disjointed boxes.

Enter: Handles.

If you don’t know about them, please check the link and learn about them. It is instrumental!

With the implementation of handles, this is how it looked in the Edit-mode

It was super easy to visualize the path and make changes to them.

All’s well that ends well

That solved the problem, and now I am excited to move on to the next step. I still have one week left in this sprint, and I hope to prototype some more crucial pieces of the mechanic.

So that’s the update. See you in the next one!

I wish you very well!

Originally published at https://www.theearlyowl.com on September 18, 2021.

--

--

EarlyOwl

📱 App coder, 🎮 Game creator, 📖 Storyteller - Sharing all that I learn along the way! Sincere hobbyist | Forever a Work in Progress | Learning out Loud