Hello everyone,
Here’s my problem: I rotate a grid and a rectangle3D (in the grid) to the left.
After 4 rotations, everything works (the 3D rectangle returns to its initial position).
The problem is that when I do 1, 2 or 3 rotations, the grid is well updated when I do an OnMouseDown of a pentamino all goes well.
But when I do an OnMouseUP the pentamino moves between the position of the OnMouseDown and the OnMouseUp and doesn’t update the grid.
What’s more, after doing an OnMouseUp (the button is released) the 3D rectangle follows the cursor in a sort of mouse trail.
Here’s my code:
void InitializeGrid()
{
for (int i = 0; i < GridSizeZ; i++)
{
for (int j = 0; j < GridSizeX; j++)
{
grid[i, j] = 0;
}
}
}
void OnMouseDown()
{
var changedFigures = Figures.Where(i => i.hasChanged);
if (changedFigures.Any())
{
selectedFigure = changedFigures.First();
offset = selectedFigure.position - GetMouseWorldPos();
foreach (Transform box in selectedFigure)
{
Vector3 firstGridSquarePosition = grilleRouge.objArray[0].transform.position;
int x = Mathf.FloorToInt((box.position.x - (firstGridSquarePosition.x - 0.8f)) / 1.6f);
int z = Mathf.FloorToInt((box.position.z - (firstGridSquarePosition.z - 0.8f)) / 1.6f);
IsValidPosition(x, z);
// if (x >= 0 && x < GridSizeX && z >= 0 && z < GridSizeZ)
{
grid[z, x]--;
}
}
}
}
void OnMouseUp()
{
if (selectedFigure != null)
{
foreach (Transform box in selectedFigure)
{
Vector3 firstGridSquarePosition = grilleRouge.objArray[0].transform.position;
int x = Mathf.FloorToInt((box.position.x - (firstGridSquarePosition.x - 0.8f)) / 1.6f);
int z = Mathf.FloorToInt((box.position.z - (firstGridSquarePosition.z - 0.8f)) / 1.6f);
IsValidPosition(x, z);
// if (x >= 0 && x < GridSizeX && z >= 0 && z < GridSizeZ)
{
grid[z, x]++;
}
}
selectedFigure = null;
}
}
void Update()
{
if (selectedFigure != null)
{
Vector3 mousePos = GetMouseWorldPos() + offset;
selectedFigure.position = new Vector3(Mathf.Round(mousePos.x / 1.6f) * 1.6f, selectedFigure.position.y, Mathf.Round(mousePos.z / 1.6f) * 1.6f);
// Optionnel : Mettez à jour l'état visuel, par exemple, changer la couleur si la position est valide ou non.
}
}
Vector3 GetMouseWorldPos()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = 10.0f; // Distance de la caméra au plan de l'objet
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void UpdateGlobalGrid()
{
// Réinitialiser la grille globale
Array.Clear(grid, 0, grid.Length);
// Ajouter chaque pentamino à la grille globale
foreach (var figure in Figures)
{
foreach (Transform box in figure)
{
Vector3 firstGridSquarePosition = grilleRouge.objArray[0].transform.position;
int x = Mathf.FloorToInt((box.position.x - (firstGridSquarePosition.x - 0.8f)) / 1.6f);
int z = Mathf.FloorToInt((box.position.z - (firstGridSquarePosition.z - 0.8f)) / 1.6f);
if (x >= 0 && x < GridSizeX && z >= 0 && z < GridSizeZ)
{
grid[z, x]++;
}
}
}
}
bool IsValidPosition(int x, int z)
{
return x >= 0 && x < grid.GetLength(1) && z >= 0 && z < grid.GetLength(0);
}
And here’s a video that says it all
Thanks for your help,
A+