• Unity
  • Animation does not play when character moved on y axis

Hi, Hope somebody can help. I am using the following code to play an animation when the player moves down:


void PlayerMovementVertical()
    {

    if (moveVelocity.y < 0)
    {
        if (skeletonAnimation.skeleton.data.FindSkin("Front") != null)
        {
            skeletonAnimation.skeleton.SetSkin("Front");
            skeletonAnimation.state.SetAnimation(0, "Walk_front", true);
        }
    }
}

(this function is called in Update)

Unfortunately, whilst the arrow is pressed the player moves down but the animation does not play whilst it is moving. However, the animation starts playing when the arrow key is no longer being pressed.

I cannot figure out what is causing the problem. Any help is appreciated.

Thanks

Related Discussions
...

You are a setting the Walk_front animation every frame. You should only set the animation if the animation you want is not already playing. You can check the current animation with AnimationState getCurrent. For example:

if (skeletonAnimation.state.GetCurrent().Animation.name != "Walk_front") {
   skeletonAnimation.state.SetAnimation(0, "Walk_front", true);
}

Thank you very much for your help. However, I had to access the animation property to confirm what animation was playing. I mention it in case somebody has the issue in future and checks the thread.


if (skeletonAnimation.state.GetCurrent(0).Animation.name!=("Walk_front"))

The odd thing is that I have other animations that run from the Update() function and I have not encountered this issue i.e. the animation not running because it has been set more than once.

.

Ah, good catch on the animation property.

Your problem isn't that the animation is not being applied. Every time you set the animation, you are starting it over. If you start it over every frame, you will only ever see the very start of your animation.