Search Unity

Stop Animated Tile from looping C#

Discussion in '2D' started by KristianKostov, Jan 19, 2019.

  1. KristianKostov

    KristianKostov

    Joined:
    Nov 26, 2016
    Posts:
    2
    I am using the animated Tile script and starting the animation when I need it, by replacing a static tile with an animated one. Currently on collision with the player, however, I need the animation to run to its end and then stop when it finishes.

    My current code is trying to find if the current sprite on the position is the same as the last one of the animation and if that is true it should just replace it with a static tile. However, that happens instantly after the animation has been triggered.

    Check if the animation is finished:
    Code (CSharp):
    1. if (playingAnimation)
    2.     {
    3.         if (tilemap.GetSprite(lastPPosition) == destructableLast.sprite) // if animation ended
    4.         {
    5.             Debug.LogWarning("Destructable Animation finished; Returning to end sprite");
    6.             tilemap.SetTile(lastPPosition, destructableLast);
    7.             playingAnimation = false;
    8.         }
    9.     }
    Code for setting the animated tile:

    Code (CSharp):
    1. private void AnimatedTile_Destroyable(TileBase type, Collision2D collision, Vector3Int pPos)
    2. {
    3.     if (type == destructable0)
    4.     {
    5.         tilemap.SetTile(pPos, destructable1Animation);
    6.         lastPPosition = pPos;
    7.         playingAnimation = true;
    8.     }
    9. }
    I did find this in the Animated Tile script:

    Code (CSharp):
    1. public override void GetTileData(Vector3Int location, ITilemap tileMap, ref TileData tileData)
    2.     {
    3.         tileData.transform = Matrix4x4.identity;
    4.         tileData.color = Color.white;
    5.         if (m_AnimatedSprites != null && m_AnimatedSprites.Length > 0)
    6.         {
    7.             tileData.sprite = m_AnimatedSprites[m_AnimatedSprites.Length - 1];
    8.             tileData.colliderType = m_TileColliderType;
    9.         }
    10.     }
    Which confuses me, because from what I see, the sprite that is used as the animated tile sprite is the last one from the animation. Shouldn't the sprite change constantly when the animation plays, and is there a way to stop the animation once it has finished?
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, currently there is no functionality to stop the animation once it has finished inside the Tilemap or to identify that as well. Is that something that is useful to you?

    Code (CSharp):
    1.  
    2.             tileData.sprite = m_AnimatedSprites[m_AnimatedSprites.Length - 1];
    3.  
    This is a placeholder Sprite that is used to fill in the cell in Edit mode. This is replaced by the animation during Play mode, so technically you could change it to any Sprite you like.
     
  3. KristianKostov

    KristianKostov

    Joined:
    Nov 26, 2016
    Posts:
    2
    Yes, I would love to have more freedom with the animated tiles. Even just having the ability to stop/play/check the animation would be a great thing to have :)
     
    mgb_pmg, HofiOne and N4ma3 like this.
  4. kayroice

    kayroice

    Joined:
    Feb 7, 2017
    Posts:
    49
    @ChuanXin any chance that we can get 'stopping tile animations during runtime' as a feature?

    I currently have a use case where tiles with animation are placed, tile(s) is animating, but then a dynamic event occurs that requires a specific tile's animation to stop. In this use case I want to stop the animation on just a single tile at a specific cell position on the Tilemap.

    For now, are there any work arounds to stopping animation on a specific tile during runtime? The only workaround I've been able to implement is setting the tile to a new tile. Unfortunately this doesn't work for me as it replaces the existing tile's instanced game object (which breaks running components when the game object is replaced). Maybe there's a way to get the TileBase at the cell position where one wants to stop animation, and set it's TileAnimationData to something that would stop the animation?
     
  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    set the static tile on the same x,y position but different z, so it will display above the old

    you shouldnt be running logic on objects that you have to destroy/replace, have all the tiles connected to a main object that processes events
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Sorry, looks like this was missed out! We will add this to be worked on. Could you share which version of the Unity Editor you are using?

    I don't think there is good workaround for this. You could manually freeze the Animation Frame for that position, but you would need to do it every frame, which is terrible.

    If you are using 2021.x, would it be possible to add the TileFlag TileFlags.KeepGameObjectRuntimeOnly for the AnimatedTile? This should keep the instanced GameObject alive for your correct workaround when you replace the Tile.
     
    mgb_pmg likes this.
  7. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    You can do it by calculating the timeout required to show all the frames, then at that point in time, refresh the tile but have the GetTileAnimationData return false, and have GetTIleData provide whatever static sprite you want to show. I have also done this using a new Tilemap method in 2022.1 beta (I think that's the one) but it turns out to be inefficient.
     
  8. kayroice

    kayroice

    Joined:
    Feb 7, 2017
    Posts:
    49
    @ChuanXin I'm on 2021.2.16f1 with 2D Tilemap Editor 1.0.0 and 2D Tilemap Extras 2.2.1. And to be clear, the functionality that I'm looking for is stopping animation on a Tile at a specific cell position, not all Tiles for a given Tilemap (or all Tiles that share the same underlying TileBase/scriptable object). Bonus points for being able to remove or change the sprite from a Tile at a specific cell position (would that also be possible?). Thanks for offering to help out with the feature request!

    I worked around the issue by creating the Tile(s) with no sprite (null), and having the Tile's instanced game object control how the Tile looks; the instanced game object has a SpriteRenderer and an Animator. When the Tile changes state to where the animation needs to stop it's easy enough to just have the Animator stop the animation for that particular Tile.

    @vonchor thanks for the suggestion. I started going down a similar path yesterday (passing in a null sprite via GetTileData after the state change), but decided to go the route of using an Animator on the Tile's instanced game object. I wasn't aware that returning false from GetTileAnimationData would stop the animation (in addition to changing the sprite and then refreshing it), is that correct?
     
    Last edited: Mar 24, 2022
  9. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    Check out the free asset referenced in my signature. Animation control on a single tile. It’s explained in the accompanying docs. I provide two types of animated tiles: one similar to the one in tilemap extras and an alternate using an asset file to list the animated sprites. That one lets you change animation sequences on the fly too. The asset in the project folder doesn’t matter anymore. But it is a whole system as well so it might be too much for your needs.
    I am traveling so excuse terseness as I’m typing this in a bouncy train.
     
  10. kayroice

    kayroice

    Joined:
    Feb 7, 2017
    Posts:
    49
    For anyone following this thread and needing additional Tilemap animation functionality, you might want to chime in on the URP 12 thread, and make your request there. Unity recently posted a roadmap for Tilemap API improvements, and there's some animation functionality being added. Now would be the time to request new features.
     
    mgb_pmg likes this.
  11. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    Check out 2022.2 https://unity3d.com/unity/alpha/2022.2.0a13