Search Unity

Tile Map How to scale (animate) individual tile at runtime ?

Discussion in '2D Experimental Preview' started by Aladine, Jan 6, 2019.

  1. Aladine


    Jul 31, 2013

    Am making a simple match3 game, with tile swapping ability, so far everything works fine but i just want to add some juice to it, am wondering if i can have the Tiles shrink and grow up directly ?

    Am using this method to do the switching:

    Code (CSharp):
    1.  public void SwitchTiles(Tilemap _map, Vector3Int pos1, Vector3Int pos2)
    2.     {
    3.         //save selected Tile
    4.         TileBase selectedT = _map.GetTile(pos1);
    5.         //save target tile
    6.         TileBase targetT = _map.GetTile(pos2);
    7.         //place selected tile to the target position
    8.         _map.SetTile(pos2, selectedT);
    9.         //place target Tile to the selected position
    10.         _map.SetTile(pos1, targetT);
    11.     }
    Am wondering if there is a way to scale (and animate) an individual tile based on giving position ?

    I tried this :

    Code (CSharp):
    1. public void ScaleTileTest()
    2.     {
    3.         Tile t = _pointTile.GetTile(_testPos) as Tile;
    4.         t.gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    5.     }
    but the casting from TileBase (GetTile()) to Tile didn't work.

    So far the only solution i can think of, is when doing the Switching, i'll hide the real tiles, instantiate Gameobjects to their positions, do the animation with these Gameobjects, and when its done, hide these objects and make the real tiles appear, but this feel like some useless extra work cause their must be a way to scale those tiles isn't ?

  2. ChuanXin


    Unity Technologies

    Apr 7, 2015
    If the Tile is set on the Tilemap with a particular position, you could try the following to scale it on the Tilemap:

    Code (CSharp):
    2. public void ScaleTile(Tilemap tilemap, Vector3Int position, Vector3 scale)
    3. {
    4.     var tileMatrix = tilemap.GetTransformMatrix(position);
    5.     var scaleMatrix = tileMatrix * Matrix4x4.Scale(scale);
    6.     tilemap.SetTransformMatrix(position, scaleMatrix);
    7. }
    Unfortunately, to animate this, you may have to do this manually as the current Tile Animation does not support transform changes.
    Aladine likes this.