0
\$\begingroup\$

I am creating a game in Unity 3d that is using an animation to move an object. For some reason the animation only works the first time it is accessed and from then on it acts as if it is not being called even though I have debugged and I see that it is being called. Here is the code I use to call the animation:

if (anim == null)
{
    foreach (Transform component in myObject.transform)
    {
        anim = components.GetComponent<Animator>();
    }
}
anim.Play("Flip-Forward");
anim.enabled = true;

As you can see I have an animator connected to myObject that I access in order to plan an animation. "Flip-Forward" is one of the animations I have created.

enter image description here

\$\endgroup\$
2
\$\begingroup\$

Animator.Play() works with the state in the Animator not the actual animation. I'm guessing you are not returning from the Flip-Forward state. Make sure to create a transition from to that state back to the default state and that the transition Has Exit Time property be ticked.

This is a simple example of how the state machine should look. However I suggest that you use triggers on the transitions between states rather than playing the states directly. Also make sure to pass 0 to the Play() method after the state name to ensure you are using the base layer.enter image description here

\$\endgroup\$
  • \$\begingroup\$ @UriPopv I am pretty new to the animation system. You say to create a transition to the default state. Should I have a different state that is connected to the "Entry" state that then connects to all other states here?This is just how the states were set up as I created them. I added a snippet of my state machine. \$\endgroup\$ – sabo Apr 25 '16 at 15:12
  • \$\begingroup\$ I edited my answer a bit hope it helps \$\endgroup\$ – Uri Popov Apr 26 '16 at 8:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.