Hi everyone,
I’m trying to put 1’s in my 8x8 grid (int [, ] grid) where the pentamino X has been dropped (in the grid).
The rest (empty cells) are worth 0.
I use a RayCast that starts from the center of the pentamino and goes down to touch the grid planes and find out which grid cell has been touched (from 0 to 63).
To do this, I use the following code:
void OnMouseUp()
{
UpdateRowAndCol();
UpdateGridWithPentaminoX(row, col); // Mettre à jour la grille avec les valeurs de ROW et COL
PrintGrid();
PrintGrid2();
PrintGrid();
}
int index;
void UpdateRowAndCol()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask))
{
if (hitInfo.collider.gameObject != gameObject)
{
if (int.TryParse(hitInfo.transform.gameObject.name, out index))
{
row = (index - 1) / 8; // Mettre à jour row
col = (index - 1) % 8; // Mettre à jour col
if (col == 0)
{
col = 8;
}
}
}
}
}
void Update()
{
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out hit))
{
Debug.DrawRay(cube.transform.position, Vector3.down * 20, Color.green);
Debug.Log("HIT: Plan touché = " + hit.transform.gameObject.name + ", Row = " + row + ", Col = " + col);
}
else
{
Debug.DrawRay(cube.transform.position, Vector3.down * 20, Color.red);
Debug.Log("Le rayon ne touche pas d'objet.");
}
}
void UpdateGridWithPentaminoX(int row, int col)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (pentaminoX[i, j] == 1)
{
// Vérifier si les coordonnées sont à l'intérieur des limites de la grille
if (row + i >= 0 && row + i < 8 && col + j >= 0 && col + j < 8)
{
grid[row + i, col + j] = 1;
}
}
}
}
}
void PrintGrid()
{
string gridStr = "Grid:\n";
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
gridStr += grid[i, j] + " ";
}
gridStr += "\n";
}
Debug.Log(gridStr);
Debug.Log("HIT: Plan touché = " + hit.transform.gameObject.name + ", Row = " + row + ", Col = " + col);
}
The problem is that Debug.Log() always returns 0 in the Row and Col.
Here’s how to set up Debug.Log():
Debug.Log("Row = " + row + ", Col = " + col); (they are calculated in void UpdateRowAndCol())
But it still returns 0.
Here’s an image showing pentamino X and its raycast (the latter is red but when it encounters a grid plane it turns green (see void Update) and returns a number from 0 to 63 (the names of small planes that form the grid). The small planes that form the red grid have a size of 1.6f (like the pentamino cubes).
Thanks for your help,
A+