Hello to all,
I managed to make the following code work:
void CreateGrid()
{
float CellX, CellY;
for (float i = -Lignes / 2, i2 = 0; i < Lignes / 2; i++ , i2++ )
for (float j = -Colonnes / 2, j2 = 0; j < Colonnes / 2; j++ , j2++ )
{
Vector3 worldPosition = new Vector3(i, 0f, j) * 1.6f;
var obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity, transform);
CellX = i2;
CellY = j2;
obj.transform.parent = empty.transform;
obj.name = "cube" + k;
k++;
}
}
The problem is that I don’t know how to use the variable hit.transform.gameObject.name (numbered with k) (which represents the number of the box that was clicked).
The second problem is CellX only returns the values of the public variable Rows and CellY the value of the public variable Columns.
In short I can’t get the rows and columns of hit.transform.gameObject.name
- I’m looking for a mathematical formula (see below)
float worldPositionX = ... from the raycast above
worldPositionX -= CenterX ; // where you consider the "zero" line of cells
worldPositionX /= CellXSize ; // central spacing of the cells
int cellX = (int)worldPositionX ;
- only I don’t understand what it means
float worldPositionX = ... from the above raycast
-
as well as CenterX (can you help me?)
-
I don’t see why you should override the worldPositionX variable.
On the other hand I assume that CellXSize = 1.6f (grid cell width)
Thanks for your help,
A+