Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get if a tilemap tile is flipped

Discussion in '2D' started by CoughE, Sep 29, 2021.

  1. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    So you can rotate tiles in a tilemap by pressing [ or ], which can then be retrieved via
    Code (CSharp):
    1. tilemap.GetTransformMatrix(Vector3Int).rotation
    But you can also flip a tile via shift + [ or shift + ]. How does one get if a tile is flipped via code?
     
  2. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Hey!
    I am pretty sure that you could find something in the TileBase.GetTileData (https://docs.unity3d.com/ScriptReference/Tilemaps.TileData-transform.html) from the tileData (https://docs.unity3d.com/ScriptReference/Tilemaps.TileBase.GetTileData.html).

    You could probably check the diagonal values of this matrix. Top-left is x, next one is y. If any of those is negative it means that the Tile is flipped on this axis. https://www.brainvoyager.com/bv/doc...Transforms/SpatialTransformationMatrices.html

    Still maybe there is a better way.

    I hope it helps!
     
    CoughE and eses like this.
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi,

    I think @SunnyValleyStudio is probably right (BTW - good YT tutorial channel!).

    Personally I haven't done such thing in the past, but you can get and set tilemap tile values. To get a tile use its position:
    Code (CSharp):
    1. var pos = new Vector3Int(1, 1, 0);
    2. var tile = tilemap.GetTile(pos);

    Then you can rotate and flip it, flipping can be done at least with negative scaling like mentioned already:
    Code (CSharp):
    1. // flip
    2. Matrix4x4 matrixFlip = Matrix4x4.Scale(new Vector3(-1f, 1f, 1f));
    3. tilemap.SetTransformMatrix(pos, matrixFlip);
    4. tilemap.RefreshTile(pos);

    Then you can get the transform of some tile by using tile position again. Proceed based on its value (*in this case I'm only Debug.Logging it):
    Code (CSharp):
    1. // get value
    2. Debug.Log("transform is: " + tilemap.GetTransformMatrix(pos));
    3.  
    4. // transform is:
    5. // -1.00000 0.00000 0.00000 0.00000
    6. //  0.00000 1.00000 0.00000 0.00000
    7. //  0.00000 0.00000 1.00000 0.00000
    8. //  0.00000 0.00000 0.00000 1.00000
     
    Last edited: Sep 30, 2021
    CoughE likes this.
  4. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    Thanks for the helpful info! Had to teach myself some matrices but I think I figured it out.
    A non flipped, non rotated tile has the matrix value of

    Code (CSharp):
    1. 1.00000    0.00000    0.00000    0.00000
    2. 0.00000    1.00000    0.00000    0.00000
    3. 0.00000    0.00000    1.00000    0.00000
    4. 0.00000    0.00000    0.00000    1.00000
    A tile flipped on the X axis has a matrix value of

    Code (CSharp):
    1. -1.00000   0.00000   0.00000   0.00000
    2. 0.00000   1.00000   0.00000   0.00000
    3. 0.00000   0.00000   1.00000   0.00000
    4. 0.00000   0.00000   0.00000   1.00000
    5.  
    and finally a tile flipped on the Y axis has a matrix value of

    Code (CSharp):
    1. 1.00000    0.00000    0.00000    0.00000
    2. 0.00000    -1.00000    0.00000    0.00000
    3. 0.00000    0.00000    1.00000    0.00000
    4. 0.00000    0.00000    0.00000    1.00000
    5.  
    So to get if a tile is flipped, simply check if
    Code (CSharp):
    1. tilemap.GetTransformMatrix(pos).m00 == -1 //X axis
    2. tilemap.GetTransformMatrix(pos).m11 == -1 //Y axis
    3.  
    4.  
    There is really no distinction in the backend if a tile is flipped or rotated as both simply update this matrix as it seems.
     
    SunnyValleyStudio likes this.