• RuntimesUnity
  • Can't stop last Spine animation looping endlessly

Hi, I have been programming for a while but am very new to Unity and Spine.

I set my object's animation state like:
if (AnimationString.Equals("Dead"))
{
enemySkeleton.AnimationState.SetAnimation(0, AnimationString, false);
}

And I can't stop it from keep looping this animation.

1) I tried the SetAnimation() with the loop argument = false
2) I tried AnimationState.ClearTrack()
3) only way to stop was manually waiting some time using a timer and doing
enemySkeleton.AnimationState.TimeScale = 0;
which I want to avoid
4) I tried
enemySkeleton.AnimationState.AddAnimation(0, (Spine.Animation)null, false, 0);
And it forces the object to stop after death animation but I want to avoid this and if I catch the ArgumentNullException the animation starts looping again.

I did the pretty same thing for my character object and it stops on death so I am very confused. Any help??

    Related Discussions
    ...

    It sounds like you are encountering an issue where the animation continues to loop despite setting the loop argument to false. Here are a few things you can try to resolve this issue:

    1. Ensure Correct Animation State Handling:
      Make sure you are not calling SetAnimation repeatedly in your update loop or any other method that gets called frequently. This can cause the animation to restart continuously.

    2. Clear Tracks and Set to Setup Pose:
      After the death animation completes, you can clear the tracks and set the skeleton to its setup pose to ensure no animations are playing:

         enemySkeleton.AnimationState.ClearTracks();
         enemySkeleton.Skeleton.SetToSetupPose();
    3. Check for Animation Completion:
      You can use the Complete event to detect when the animation has finished and then clear the tracks:

         var trackEntry = enemySkeleton.AnimationState.SetAnimation(0, AnimationString, false);
         trackEntry.Complete += (entry) => {
             enemySkeleton.AnimationState.ClearTracks();
             enemySkeleton.Skeleton.SetToSetupPose();
         };
    4. Avoid Infinite Loops in Callbacks:
      Ensure you are not creating an infinite loop by calling SetAnimation or SetEmptyAnimation within the Complete or End event callbacks. This can cause the animation to loop endlessly.

    5. Debugging:
      Add some debug logs to ensure that your SetAnimation call is not being overridden or called multiple times:

         Debug.Log("Setting animation to Dead");
         var trackEntry = enemySkeleton.AnimationState.SetAnimation(0, AnimationString, false);
         trackEntry.Complete += (entry) => {
             Debug.Log("Animation complete");
             enemySkeleton.AnimationState.ClearTracks();
             enemySkeleton.Skeleton.SetToSetupPose();
         };

    By following these steps, you should be able to stop the animation from looping endlessly. If the issue persists, it might be helpful to create a minimal Unity project that demonstrates the problem for further debugging.

    hh1834 Could you please show us the whole code, not just snippets? The SetAnimation() method itself is not written wrong, so I think the way it is called is probably wrong. For example, calling it in Update() may cause SetAnimation() to be called several times until the AnimationString is updated, causing playback of the animation to go wrong.

      • Düzenlendi

      Misaki I think the SetAnimation() method is called once for the death animation because I have been carefully checking the logs for whenever the object's state changes. Also if SetAnimation() method was being called constantly or something it would keep playing the first frame of the animation and look very awkward. But for me, death animation is playing from the start to end, from the start to end, and on and on.

      Is the way suggested by the Spinebot above the correct way to add a listener? I did

                  trackEntry.Complete += (entry) => {
                      Debug.Log("---------------");
                      enemySkeleton.timeScale = 0;
                  };

      in the hope that the animation would stop after 1 completion but it doesn't seem like it is doing that

        hh1834 I think Spinebot's suggested code for debugging itself is correct, but there should be no need to make it that complicated in the first place. If the animation is only set once by SetAnimation() and loop is false, it should not loop. Since we can't understand the cause if you only share part of the code, please show us the whole script or email us a minimal Unity project that can reproduce the problem: contact@esotericsoftware.com

          Misaki I finally figured it out... You were right that the way it was being called was incorrect:

          Basically, in the series of "if" and "else if" checks, one of them should have been "else if" but it was set to "if", causing the "else" clause at the end to call the animation change again with loop=true...

          Sorry for the trouble. And thank you for the help!

            hh1834 I'm glad you found the cause! Thank you for getting back to us. 🙂