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\$

1 Answer 1

3
\$\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\$
2
  • \$\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, 2016 at 15:12
  • \$\begingroup\$ I edited my answer a bit hope it helps \$\endgroup\$
    – Uri Popov
    Apr 26, 2016 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .