Get if a tilemap tile is flipped

So you can rotate tiles in a tilemap by pressing [ or ], which can then be retrieved via

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?

Hey!
I am pretty sure that you could find something in the TileBase.GetTileData (Unity - Scripting API: Tilemaps.TileData.transform) from the tileData (Unity - Scripting API: Tilemaps.TileBase.GetTileData).

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. Spatial Transformation Matrices

Still maybe there is a better way.

I hope it helps!

2 Likes

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:

var pos = new Vector3Int(1, 1, 0);
var tile = tilemap.GetTile(pos);

Then you can rotate and flip it, flipping can be done at least with negative scaling like mentioned already:

// flip
Matrix4x4 matrixFlip = Matrix4x4.Scale(new Vector3(-1f, 1f, 1f));
tilemap.SetTransformMatrix(pos, matrixFlip);
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):

// get value
Debug.Log("transform is: " + tilemap.GetTransformMatrix(pos));

// transform is:
// -1.00000 0.00000 0.00000 0.00000
//  0.00000 1.00000 0.00000 0.00000
//  0.00000 0.00000 1.00000 0.00000
//  0.00000 0.00000 0.00000 1.00000
1 Like

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

1.00000    0.00000    0.00000    0.00000
0.00000    1.00000    0.00000    0.00000
0.00000    0.00000    1.00000    0.00000
0.00000    0.00000    0.00000    1.00000

A tile flipped on the X axis has a matrix value of

-1.00000   0.00000   0.00000   0.00000
0.00000   1.00000   0.00000   0.00000
0.00000   0.00000   1.00000   0.00000
0.00000   0.00000   0.00000   1.00000

and finally a tile flipped on the Y axis has a matrix value of

1.00000    0.00000    0.00000    0.00000
0.00000    -1.00000    0.00000    0.00000
0.00000    0.00000    1.00000    0.00000
0.00000    0.00000    0.00000    1.00000

So to get if a tile is flipped, simply check if

tilemap.GetTransformMatrix(pos).m00 == -1 //X axis
tilemap.GetTransformMatrix(pos).m11 == -1 //Y axis

There is really no distinction in the backend if a tile is flipped or rotated as both simply update this matrix as it seems.

1 Like