Pathfinding
By Jay Marriner, 27/10/2022
Following on from my previous blog post about creating a life simulator; I endeavoured over the last few days to create a system of being able to create cities easily. This would allow both developers on the game to create new areas easily and potentially allow players to create custom areas if the tool can be polished to a high standard.

Path finding

The first challenge was the toughest one - creating a path finding system. This was necessary to build roads, guide AI and many other functions. Unity has built in systems for path finding however I wanted to learn for myself how to create a system - which also gives me more control over how it works. Starting off I created a simple grid script that stores no physical grid but returns a grid position based on parameters set ahead of time (mainly the gap size). After this I created a place items script that allowed basic items to be placed into the scene and snap to a grid position. Now the basics were in place I spent a few days developing (and mostly learning) how simple path finding works. Whilst the final result isn't greatly optimised (yet) it does work very well and even have a parameter for setting the cost of doing diagonal movement (this will be important when switching between roads, paths, NPC movement, etc).

Saving prefabs

As the path finding took a lot of time and effort to create I was wanting to create something I had more confidence in and so I settled on being able to create a prefab using the scene objects so world layouts could be saved down the line for setting world maps or other uses. Currently it doesn't save the grid list but this would be an easy remedy that I will definitely add in the future to ensure they are factored into the path finding when added to a scene.

Other fun scripts

I ended up creating some other fun scripts whilst working on this project such as a script that systematically updates child objects with different materials depending on what purpose the object is serving (green slightly transparent for when placing, red slight transparent for when about to delete and normal opaque colours for standard scene). This will most likely need refining before it is useful for end users and it currently has a bug where textures are missing in saved prefabs on objects that were hovered for deletion but this is a task for another day...

Final words

This week I've learnt a lot; path finding is a difficult but worth while challenge to set yourself and the result of seeing the path complete is worth all the effort and headaches along the way. I can't wait to further develop this tool and hopefully create cool little towns with hopefully some NPCs in the next blog post...