I am a Unity newbie and trying to implement a simple building system in a prototype of the game. The scenario is:
- Clcik on the UI Button
- Move the coursor to the desired position
- Click left mouse button and place an object.
Game info
I had a Isometric Z as Y grid with child tilemap, already had some simply map

I had created a few scripts with separated logic and already tried to implement 2 approaches:
- Calculate Vector3Int coordinates of the mouse (x, y) - the coordinates of the tilemap's cell
Vector3Int GetMousePosition()
{
int z = (int)GridManager.Instance.GetActiveTilemap().transform.position.z;
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 mouseWorldPosTranformed = new Vector3(mouseWorldPos.x, mouseWorldPos.y, z);
Vector3Int pos3d = grid.WorldToCell(mouseWorldPosTranformed);
return pos3d;
}
then object is following by coursor by
tempObject.transform.position = position;
here 'position' is a Vector3Int output of the GetMousePosition() function
But the result looks like this:
The answer is that the object (blue diamond) is placed in a "normal" coordinates x', y' in point (4; 0).
But my grid object has an isometric layout so visually object is placed not in a grid cell
But if I change the Grid's layout to rectangular - everything match perfect.
- Calculate real coordinates of the mouse using
Vector3 GetMousePosition()
{
Tilemap map = GridManager.Instance.GetActiveTilemap();
int z = (int)map.transform.position.z;
Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 cellPos = GridManager.grid.GetComponent<GridLayout>
().LocalToCell(touchPos);
return cellPos;
}
and then transform coordinates using grid layout and CellToLocalInterpolated().
tempObject.transform.localPosition = GridLayout.CellToLocalInterpolated(position + new Vector3(0.5f, 0.5f, 0f));
Now visually the result is correct

BUT the problem is that now the coordinates is not match grid cell position:
for example object has coordinates (1.5; 1.5) while tilemap cell is (4; 1) starting from 0 included.
I understand that this is because of Unity has only flat XY coordinates and isometric projection is just visually transformation. Nevertheless this is visually looks fine I already see a problem to match coordinate of grid cells to this coordinates of my objects.
So I am looking for advice.
For now I am thinking of separate object into 2 classes:
- ObjectModel. This class is instaintiated using 1st approach and it will have no prefab or any of visual properties.
- ObjetView. This class will have an
ObjectModel objectModelproperty, prefab properties and all visual components for visualisation and will instantiated using 2nd approach and visually everything will still looks fine.
But I am still not sure this is a good approach and maybe this could be done much more easier. Bcs for every object I should create at least 2 instance of class.
tempObject.transform.position = position;you want something liketempObject.transform.position = grid.CellToWorld(pos3d);wherepos3dis theVector3Intcell coordinate you calculated in the previous code block. \$\endgroup\$