Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tilemap isometric offset

Discussion in '2D' started by slightlygrim, Sep 29, 2022.

  1. slightlygrim

    slightlygrim

    Joined:
    Jul 10, 2017
    Posts:
    13
    Hi Guys

    Wondering if anyone can help, been starting to try to make an isometric game. Got my tilemap setup followed the instructions. I've been try to generate a map over the top of the tilemap but every time it seems to be offset. Anyone got any ideas what I'm missing?

    upload_2022-9-29_17-18-15.png

    upload_2022-9-29_17-20-58.png


    Code (CSharp):
    1. private void Start()
    2.     {
    3.         OnGridCellCreated += CreatePrefabOverlay;
    4.         CreateGrid();
    5.     }
    6.  
    7.     private void CreatePrefabOverlay(Vector3Int position)
    8.     {
    9.         var cellPosition = _tilemap.GetCellCenterWorld(position);
    10.         var tile = Instantiate(overlayTile, transform);
    11.         tile.transform.position = new Vector3(cellPosition.x, cellPosition.y, cellPosition.z);
    12.     }
    13.  
    14.     private void CreateGrid()
    15.     {
    16.         BoundsInt cellBound = _tilemap.cellBounds;
    17.         gridMap = new Dictionary<Vector2Int, GridCell>();
    18.  
    19.         for (int z = cellBound.max.z; z > cellBound.min.z; z--)
    20.         {
    21.             for (int y = cellBound.min.y; y < cellBound.max.y; y++)
    22.             {
    23.                 for (int x = cellBound.min.x; x < cellBound.max.x; x++)
    24.                 {
    25.                     var tileLocation = new Vector3Int(x, y, z);
    26.                     var tileKey = new Vector2Int(x, y);
    27.                     if (!_tilemap.HasTile(tileLocation)) continue;
    28.                     var cell = new GridCell(x, y, z);
    29.  
    30.                     if (gridMap.ContainsKey(tileKey)) continue;
    31.                     gridMap.Add(tileKey, cell);
    32.                     OnGridCellCreated?.Invoke(tileLocation);
    33.                 }
    34.             }
    35.         }
    36.     }
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, I am not quite sure what the individual elements of your Tilemap should look like (eg. Sprite, Prefab, Tile) when placed in cells separately. If you could share that, that would be helpful!

    Presumably, you would like to align the cells to the Grass portion of your Sprites? The Sprite could have its pivot set to the bottom of the Dirt portion rather than the bottom of the Grass portion.
     
  3. slightlygrim

    slightlygrim

    Joined:
    Jul 10, 2017
    Posts:
    13
    Yes I'm expecting the mapping to be positioned on top of the tilemap like this: upload_2022-9-30_9-8-42.png

    I've changed the pivot point of the white tile and the pivot point of the tilemap and the pivot point of the grass but nothing seems to get me to the right positioning
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share the pivot points of the Sprites you are using?
     
  5. slightlygrim

    slightlygrim

    Joined:
    Jul 10, 2017
    Posts:
    13
    These are the two sprites that I am using with both at center pivot point:
    upload_2022-9-30_10-55-8.png
    upload_2022-9-30_10-55-31.png
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The pivot of the Grass Sprite is set to the bottom area of the Grass. For your indicators to match, their pivots must be at the same visual location. For your White Sprite, its pivot needs to be set to the bottom center. For the Yellow target, its pivot needs to be set to the bottom center and shifted a little upwards to the top of the shadow.
     
  7. slightlygrim

    slightlygrim

    Joined:
    Jul 10, 2017
    Posts:
    13
    I've set the tile to the bottom center but I'm still a fraction off: upload_2022-10-4_10-29-34.png upload_2022-10-4_10-30-8.png
     
  8. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    That seems odd, especially if you set up the yellow target indicator the same way.

    Could you select the White Sprite/Tile in your SceneView and share screenshots of its Inspector here?