• RuntimesUnity
  • Best way to setting an animation?

Hello!
I have this code (inside the Update method), and I realised that my animation seems to not be applied, but I guess what's happening is that it's being played on the first frame over and over.
if (facingDirection.Equals(Direction.East) || facingDirection.Equals(Direction.West)) //East and West have the same animation
{
if (!isRolling)
{
track0Entry = angieSklAnim.AnimationState.SetAnimation(0, walkEAnimation, true);
}
}

What would be the best way to ensure that the animation is not set if that same animation is already playing? I fear that I might end up having to do some convoluted string comparison every time I set an animation, but there must be a better way 🙂

Thanks in advance!

  • Harald đã trả lời bài viết này.
    Related Discussions
    ...

    To ensure that an animation is not set if it's already playing, you can check the current animation on track 0 before setting a new animation. This can be done by comparing the animation name of the currently playing animation with the name of the animation you intend to play. If they are not the same, then you set the new animation. Here's how you can modify your code:

    // Assuming walkEAnimation is a string containing the name of the walking animation.
    TrackEntry currentEntry = angieSklAnim.AnimationState.GetCurrent(0);
    if (currentEntry == null || currentEntry.Animation.Name != walkEAnimation) {
        if (!isRolling) {
            track0Entry = angieSklAnim.AnimationState.SetAnimation(0, walkEAnimation, true);
        }
    }

    This approach avoids unnecessary string comparisons when the desired animation is already playing and prevents the animation from restarting each frame. It checks if there's currently an animation playing on track 0 and if the name of that animation is different from the one you want to play. If both conditions are met, it sets the new animation. This method is efficient and straightforward, ensuring that your animations transition smoothly without unnecessary restarts.

    • Đã chỉnh sửa

    pixelmeat What would be the best way to ensure that the animation is not set if that same animation is already playing? I fear that I might end up having to do some convoluted string comparison every time I set an animation, but there must be a better way 🙂

    Note that it's not the SkeletonAnimation.AnimationState which should hold information about your character's game state (that would be bad design), instead you should better e.g. write a character component for that. You need some general game-state management code which knows which state it's in, and then only performs necessary changes once upon switching to a different state, e.g. starting a different animation, changing stat values of your character, etc. If you look up "state machine" you should find loads of helpful references on the internet.

    Apart from that, you don't need to compare animations by name string, instead you could compare the Spine.Animation reference.

      Harald Thanks!

      • Harald đã thích điều này.