Moving in grids

Hey
How can I make something move in a grid? When I move it in the editor it move just from 0 to 1 (cause its in a grid) but when i tell it should move 1 in the code i moves like less or more.

invSpace.transform.Translate(new Vector2(1f, 0));

i hope u can help
Timo

If you’re using the Grid component, then you’ll want to use its functions to get positions on the grid.

Get a reference to the grid component thru the inspector

public Grid grid; // drag into inspector

Then move “cellSize” distance instead of 1f.

invSpace.transform.Translate(new Vector2(grid.cellSize, 0));

or convert to grid coordinate space, add one cell, and convert back to world space:

Vector2 gridPosition = grid.WorldToCell(transform.position); // convert to grid cell coordinate
gridPosition += Vector2.right; // add one cell ( 1,0 )
transform.position = grid.CellToWorld(gridPosition); // convert back
1 Like