How to 'map' surrounding cubes in expandable 3D grid system

I’m working on a game that starts with a 3x3 grid of nine cubes as the gameboard. Throughout the game, players can add sets of three cubes as a turn, just about anywhere that is adjacent to another cube - on the x, y and z axis and in no uniform way. Over time, the gameboard will evolve with cubes going in all directions from random places.

For each cube, I need to know how many cubes are touching a side, edge or corner. In theory, a single cube could have 26 other cubes touching it - in a 3x3x3 grid of 27 cubes, the center cube would be touching the 26 others.

Does anyone have any thoughts on the best way to approach this?

I attempted to use colliders with triggers on each cube that returned which colliders were overlapping. From there, a manager script on each cube would keep track of how many of the potential 26 touching cubes were in existence. I got inconsistent results and am now questioning using colliders like that.

Any tips or thoughts are appreciated. I know this concept can’t be unique and must be used in games with real-time building.

It’s always best to use Unity only to present the data you have in your game board.

Meanwhile, track your game board squares in a 2D array (or other collection of your choice) so that you can do logic against those cells specifically and not be concerned about colliders and other Unity-specific stuff.

That way if in the future you decide to change how the game is presented (e.g, turn it into 2D for a change), none of the game logic has to change. Only the presentation of the game board changes.

1 Like