# Creating a Special 3D Tile Map

So I’ve been working on a tile based game in unity 3D recently that I’ve somewhat run into some issues. The game is designed to be a multiplayer board game setup, where each player is in control of their half of the board and move troops towards the opponent. Originally I was using individual game objects for the tiles, but then I started using the method from this series to help optimize and simplify the later development.

The game will use two game object, one for each player, alongside a board manager script to deal with all the game functions. The first 2 rows of tiles (3 rows if counting by vertices) I want to have as larger tiles designated for building. Due to this I’ve needed to change how the mouse/tile selector functions so that it can hover over both game objects individually, as well as behave differently for these special tiles. The issue is that the selector function is designed through math rather than cross-checking a collection of recorded positions. I had thought about using a list or array of vector3’s, as well as a dictionary of these Vectors to store their tile data and just checking what the closest listed vector is to snap into place. The issues with this approach is that it seems that it would be poorly optimized, and as of right now when I use vector3’s as keys, even when I search the dictionary with the exact same vector that I entered as a key I run into the floating point error that I’ve seen people discussing. The dictionary will return an error saying that key doesn’t exist within my dictionary. So what would be a good way to approach the tile selection? Should I use a 2D array and find a way to connect it to a dictionary to store my tile data or is there a better method to approach it?

The way I do this is to round the given number to the nearest center, such that it will find it.

In the example where the centers are at integer numbers, it’s easy. That means that (0.0 and 0.1 and 0.2) would ALL map to the same coordinate 0, as would 3.0, 2.9 and 3.1… which would all map to coordinate 3.

But you could just as easily have any other arbitrary “center” coordinate to ensure the key is found for a given cell.

that’s how I have it currently set up, but I still receive the error saying the coordinates don’t exist as keys.

Time to strip your dataset down to the smallest set of tiles that shows the problem, print all the keys out, print the key you’re looking for, and track it down.

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

• is this code even running? which parts are running? how often does it run? what order does it run in?
• what are the values of the variables involved? Are they initialized? Are the values reasonable?