AnimatedTile - How to get its transform?

Hi everyone.

This is my first post here, so I hope I am doing everything alright.

Our problem:
We are using AnimatedTiles on a Tilemap in a Grid. However, the AnimatedTiles – unlike regular Tiles – don’t seem to have a transform component that is accessible via script.

Why do we need it?
Well, we are placing & rotating the AnimatedTiles to the direction that we need them in. Imagine conveyor belt tiles that move in all directions (up, down, left, right). We created one animated tile for one direction (right) and we want to reuse that tile for the other directions by rotating it in 90° angles.
It works fine in the editor where we are able to rotate the AnimatedTiles just fine, but we want to be able to read the AnimatedTile’s rotation from a Tilemap in a script to save our level to data.

Does anyone here have any pointers as to how to get the AnimatedTile rotation?

Thank you!

Tiles are derived from ScriptableObjects, not MonoBehaviours.

Their position is only meaningful in the context of the Grid and Tilemap that holds them, plus obviously what “cell” they are located in.

Tiles do actually have a transform. You can rotate, scale, and/or transform the tile’s sprite, even use Dotween if you like. The main caveat is that the Tilemap’s Chunk Culling Bounds may not work correctly. In any case, here’s something that may help - it’s a tiny part of my free tilemap toolkit asset :slight_smile:

Just create a static class of your choice and drop this in.

        /// <summary>
        /// Get the transform components for a tile. Convenience Function.
        /// </summary>
        /// <param name="map">Tilemap</param>
        /// <param name="position">position on map</param>
        /// <param name="tPosition">transform's position placed here</param>
        /// <param name="tRotation">transform's rotation placed here</param>
        /// <param name="tScale">transform's scale placed here</param>
        /// <remarks>Handy for tweening the transform (pos,scale,rot) of a tile</remarks>
        /// <remarks>No checking for whether or not a tile exists at that position</remarks>
        public static void GetTransformComponents(Tilemap map,
            Vector3Int position,
            out Vector3 tPosition,
            out Vector3 tRotation,
            out Vector3 tScale)
        {
            var transform   = map.GetTransformMatrix(position);
            tPosition    = transform.GetColumn(3);
            tRotation    = transform.rotation.eulerAngles;
            tScale       = transform.lossyScale;
        }

        /// <summary>
        /// Set the transform for a tile. Convenience function.
        /// </summary>
        /// <param name="map">tilemap</param>
        /// <param name="position">position on map</param>
        /// <param name="tPosition">position for the tile transform</param>
        /// <param name="tRotation">rotation for the tile transform</param>
        /// <param name="tScale">scale for the tile transform</param>
        /// <remarks>Handy for tweening the transform (pos,scale,rot) of a tile's sprite</remarks>
        /// <remarks>No checking for whether or not a tile exists at that position</remarks>
        public static void SetTransform(Tilemap map,
            Vector3Int position,
            Vector3 tPosition,
            Vector3 tRotation,
            Vector3 tScale)
        {
            map.SetTransformMatrix(position, Matrix4x4.TRS(tPosition, Quaternion.Euler(tRotation), tScale));
        }

Hope this helps

2 Likes

This helped greatly. I didn’t know this method existed and it gives me the rotation of every single Tile no matter the type. This solves my problem.

Thank you so much!

Nice, very useful.

There’s also a “look at” equivalent in the library where that came from. Useful when you want to have a symbol (like a pointer) that rotates to look at a target.

1 Like