1
\$\begingroup\$

I am beginner in Unity and I have created tilemap using new tilemap system in unity which looks like this:

enter image description here

I created a script that lets me swap tiles. First I click on some tile and than I choose second tile, after I click second time, tiles swap their positions. Which will than look like this:

enter image description here

How can I apply some animation to this process? Seems like I have no control over tile object as it is. I can only access TileBase which as I understand is not all information about tile. I can't create any animator controller or access its transform object. I want to add some custom animation when tiles swap their places. Thank you!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ You may want to consider that the things that animate don't have to be the literal tiles you're swapping. Game development is often about trickery and sleight of hand. You could, for instance, change both tiles to blank squares, and in the same frame spawn two sprite renderers at the tiles' exact locations, bearing their tile graphic as a sprite. Then you can animate those sprite renderers moving or changing in any way you want, then once the animation is done, destroy the sprite renderers and set the tiles to their post-swap state. It still looks to the player like the tiles animated. \$\endgroup\$
    – DMGregory
    Jan 13, 2020 at 23:44
  • \$\begingroup\$ @DMGregory thank you, that sounds interesting, i will try it. \$\endgroup\$ Jan 15, 2020 at 12:50

2 Answers 2

0
\$\begingroup\$

The Tilemap with the TilemapRender can only handle tiles at integer tile coordinates. That means that when you want to animate tiles moving across the tilemap in a fluid manner instead of step-by-step, then you have to find a different solution to render them during that movement.

So what you need to do is:

  1. Create two new game objects with sprite renderers at the locations of the tiles you want to swap
  2. Assign the sprites of those tiles to those sprite renderers (tilemap.GetSprite(position))
  3. remove both tiles from the tilemap (tilemap.SetTile(position, null);)
  4. Animate the swap with the two game objects to their respective new positions
  5. Add the tiles back to the tilemap at their new coordinates
  6. Destroy the two game objects

Oh, and don't forget to handle the edge-case of the player (or something else) interacting with tiles which are currently empty because they are in the process of being replaced. But that's a subject for a different question.

\$\endgroup\$
-1
\$\begingroup\$

You can possibly achieve tile animation by using unity's "AnimatedTile" Script which comes with the 2d-Extra repo. This video from unity discusses how to use the AnimatedTile Script. I hope it helps.

https://github.com/Unity-Technologies/2d-extras/tree/4eff73f468142907629f09b4a88bceb2f0148dc5/Runtime/Tiles/AnimatedTile

\$\endgroup\$
1
  • 1
    \$\begingroup\$ i saw video about animated tiles and i thought that it just makes one tile animated, basically waterfall animation is a list of sprites that play in some order, while i need little bit another behavior, but thank you, i will check it. \$\endgroup\$ Jan 15, 2020 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .