Math in Unity : Grid and Bitwise Operations (Part I)
After some articles about C#, we back to Unity.
Today we will talk about bit operations within a simple grid. When creating a grid in Unity, using arrays is often the first solution that comes to mind. And it’s perfectly fine.
Let’s keep it easy, so the “size” will be the same for x and y, so our grid will be a square.
To start we create a reference for our prefab (I simply use a cube with the y scale at 0.3f).
We need a size for the size of the grid (I used an 8 but feel free to choose the one you prefer).
So let’s create a GameObject array to fill our grid.
Now, in Start () we initialize the _grid array with a size of 64, or _size * _size
Then we set the nested loop to create the grid
I set up a very simple scene
The Camera Flag of the Main Camera is set to Solid Color (i use a blue) and it’s position is this
Now, if we press Play
We have our own 8 * 8 grid.
Now let’s move on to the next step: if we set the tag of each cell like this (we need to create a tag for our Cube and call it Tile)
then add this line of code
tile.name = tile.tag + “_” + _row + “_” + _column;
Each cell created will have its tag “Tile” and x and y as position in the grid
We have cell go from 0, 0 to 7, 7
In the next section we see how to set and what are the possible operations with bits.