0
\$\begingroup\$

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:

if (playingAnimation)
    {
        if (tilemap.GetSprite(lastPPosition) == destructableLast.sprite) // if animation ended
        {
            Debug.LogWarning("Destructable Animation finished; Returning to end sprite");
            tilemap.SetTile(lastPPosition, destructableLast);
            playingAnimation = false;
        }
    }

Code for setting the animated tile:

private void AnimatedTile_Destroyable(TileBase type, Collision2D collision, Vector3Int pPos)
{
    if (type == destructable0)
    {
        tilemap.SetTile(pPos, destructable1Animation);
        lastPPosition = pPos;
        playingAnimation = true;
    }
}

I did find this in the Animated Tile script:

public override void GetTileData(Vector3Int location, ITilemap tileMap, ref TileData tileData)
    {
        tileData.transform = Matrix4x4.identity;
        tileData.color = Color.white;
        if (m_AnimatedSprites != null && m_AnimatedSprites.Length > 0)
        {
            tileData.sprite = m_AnimatedSprites[m_AnimatedSprites.Length - 1];
            tileData.colliderType = m_TileColliderType;
        }
    }

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?

\$\endgroup\$
  • \$\begingroup\$ Isn't it possible that for some reason the if-statement in the last code snippet gets triggered, causing the animation to end early? \$\endgroup\$ – sjoerd216 Jan 9 at 10:44
  • \$\begingroup\$ Hmm, it shouldn't. If I just comment out the first code snippet the animation will start when the player touches the tile and will loop endlessly. So the problem should be in the first snippet. \$\endgroup\$ – Kristian Kostov Jan 9 at 11:38
  • \$\begingroup\$ Well if commenting out the first snippets makes it run, than I would imagen the issue is caused by entering the animation-end check too early. Have you debugged the results of that if-statement? \$\endgroup\$ – sjoerd216 Jan 9 at 13:25
  • \$\begingroup\$ Came to the same conclusion. However, the only think it does is check if the current sprite is the same as the one in the end of the animation. It shouldn't affect the behaviour if that is false. Thus, the GetSprite obviously gets triggered, even though the sprite is not that one. That is why I added the third snippet to the question, where I found that the animation sprite is coded to be the last animation sprite. The last snippet is from the script that comes with the 2D extras from unity, it is not written by me. I just want to check if the animation has ended and then stop it from looping. \$\endgroup\$ – Kristian Kostov Jan 9 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.