Creating a walkway building system

Hello!
I would like to create a system where the player can build walkways at runtime for a building type game I’m working on. Very similar to Rollercoaster Tycoon (3) or roads in SimCity or Cities Skylines if you’re not familiar with Rollercoaster Tycoon.

My pathway system will however be fairly simple, as it will only be used as walkways for AI pedestrians. The system will also be grid based on a flat 3D plane so there will be no need to handle curves or terrain.

Something I’ve also encountered a lot while reading is procedural mesh generation which I feel might be overkill in my case. Instead my current solution would be to create all the different pieces that is needed (straight, turn, crossroad, end) and simply instantiate the correct one on one cell of the grid. Is this a good approach? My main concern with this is clipping, something I encountered during some testing of this idea.

Which leads me into my first real issue which is how to handle and update the pieces on the grid. The best solution I’ve found myself while reading is to alert the 4 neighbouring cells (up, down, left, right) to the one placed or removed and that way update (remove and replace i assume) those cells so they fit with the new cell. This seems like a good solution but I don’t know how to implement this in a way that makes sense. You could check all directions manually one at a time, is there a walkway above me? is there one to the left on me? but this seems incredibly inefficient.

The second problem has to do with navigation. I thought I could use unity’s navmesh system initially but with instantiating prefabs I have yet to found a solution that works with unity’s navmesh system as it has to be baked before runtime. This leaves me to look at other options, what I’ve found so far and think is the best solution would be a node based navigation system with A* or Dijkstra’s algorithm. Which probably could be it’s own thread but something I thought I would include as I am unsure how to implement this with my walkway building system. Or if there is a simpler better system all together?

With that said hopefully I’ve explained myself and the problems I am having well enough for you guys to understand. If anything is unclear please let me know.
Any help is much appreciated.

Thanks :slight_smile:

You could look into Unity’s TileMap system: Unity - Manual: Tilemap component reference

As for navigation, if it’s RCT style you probably don’t need a separate thread to handle navigation. Djikstra’s would work well if your grid is not too large, and if you optimize properly, such as only recalculate paths when changing the topology of your paths, and only recalculating paths for actors who touch modified paths. I’ve done exactly that for grid-based tower defense games with realtime obstacle/tower placement.

I have actually looked at the TileMap system before and I think it would work, I haven’t tried it but I don’t know if there is any benefit of using it versus making a 2 dimensional grid yourself.

Yes the navigation will be very similar to RCT, the grid won’t be massive but not very small either so I think using Dijkstra’s as you describe is the current idea.

A really great way of doing something like this is to create a binary lookup table.
Basically you have one sprite or 3d model for every possible path (no neighbors, endpiece, straight, curve, T, cross)

When a tile gets updated (either because you placed it, or because a tile next to it got updated) the program creates a four-digit Bit array based on its surroundings: The first bit represents whether there is another path tile north of the tile, the second whether there is one to the east, the third to the south and the fourth to the west.

You can then treat that four-digit bit array as a binary number and turn it into an integer between 0 and 15 like this:

            int[] temp = new int[1];
            bitArray.CopyTo(temp, 0);

            int lookupIndex = result[0];

The integer then serves as an index in an array like this one from my own version that I made for a recent project:

        static readonly int[,] lookUpTable = new int[16, 2]
        {{0 ,0 },
        {2 ,0 },
        {2 ,1},
        {1 ,0 },
        {2 ,0 },
        {2 ,0 },
        {1 ,3 },
        {3 ,0 },
        {2 ,1 },
        {1 ,1 },
        {2 ,1 },
        {3 ,1 },
        {1 ,2 },
        {3, 2 },
        {3, 3 },
        {0, 0 }};

In this case the first number in the array entry tells you which version of the model to use (0=cross, 1= curve, 2=straight, 3=T-piece) and the second the needed rotation divided by 90°.

Filling the array is a bit tedious and error-prone but apart from that this is a really easy and performant way of doing this.

A lookup table did cross my mind as a possible solution. I think if i understood correctly, the problem with this solution is that you don’t have any reference to the neighbouring cells/tiles. I guess you know what position you placed the current tile on and therefore that way get a reference to the neighbours to update them.
Very interesting solution, thanks.

No matrer what you do, youll always have to have some kind of system that allows you to check which grod positions have which tiles on them.

If your map is relatively small and most (or all) grid positions have a tile one them, you can just use a 2D array.

However, if most of the positions are empty, you’d waste a lot of memory that way. In that case I’d use a dictionary using the grid coordinate (as a Vector3Int or Vector2Int) as the key.

For very karge maps you should propbably also create a chunk system, but for smaller ines that’s not necessary.