# Need some help with Unity 2d animation controller

So my animations don't transition right. I have a 2d top down prototype project and animations for 4 directional movement and an idle position. Right now i have the movement set up so the right animation activates when the player moves in one of 4 directions. problem is that the walking animation does not stop when the player stops, it doesn't transition over to the idle animation.

My animation controller looks like this:

The parameter between the animations are an integer that signals which direction needs to play, from 0 to 3. I have been messing around with a speed float parameter, but I don't really know how to get it to work.

if (Input.GetKey (KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed,GetComponent<Rigidbody2D>().velocity.y);
animator.SetInteger("Direction", 2);
animator.SetFloat("MoveSpeed", 0);
}

if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
animator.SetInteger("Direction", 0);
animator.SetFloat("MoveSpeed", 0);
}

if (Input.GetKey(KeyCode.W))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, moveSpeed);
animator.SetInteger("Direction", 1);
animator.SetFloat("MoveSpeed", 0);
}

if (Input.GetKey(KeyCode.S))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, -moveSpeed);
animator.SetInteger("Direction", 3);
animator.SetFloat("MoveSpeed", 0);
}


Here is my movement code if it helps.

If I'm missing something important that you need to help me, please tell me.

• By the way: I find the Unity animation system fine for 3d models, but far too overengineered for 2d. Especially when you want to reuse the same animation for multiple spritesheets. I found it far easier to write my own animation controller. – Philipp Mar 21 '17 at 14:39

From what I can get from that picture and your code, I can understand that your transition from "Any State" to, for example, "walk_left" is when Direction is 0, right? Problem is that you're not setting the Direction value to another value when you stop holding the key, so it keeps going back to "walk_left" over and over again, so of course it's never going to get to idle. What you can do is, either create another animator parameter that tells the Animator when you're walking, for example a isWalking bool. Or use another value for when you're not walking, -1, 4, whatever you want.

Then, in the transitions to idle use that new value as the condition. If using the boolean, remember to check that isWalking is true before transitioning to any of the walking states, and that isWalking is false when you need to transition to the idle states.

As for how to know when you stopped walking you can use something like this:

if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0f, 0f);
animator.SetInteger("Direction", 4);
animator.SetFloat("MoveSpeed", 0);
}

• It's worth noting that if you want to idle in different directions, youll need to split the GetKeyUps to their own conditions. As you've written it, the character will always idle in direction 4. – Stephan Apr 11 '17 at 15:48
• Not really. The "Direction 4" is used there for an "Idle" state, independent of direction. The 4 directions would be 0,1,2,3. By using the condition of "Direction == 4" in each walking state, the correct idle state is played. – César Augusto Apr 12 '17 at 18:45
• Ah ok, i misunderstood – Stephan Apr 12 '17 at 19:56
• Hah that's okay, it took me quiiiiiiiite a while to understand how the Animation Controller worked, and there's still quite a few things I don't understand. – César Augusto Apr 12 '17 at 20:12

I handled one of mine as follows:

int direction=0; //0,1,2,3=N,E,S,W
bool walking = false;
...
if(Input.GetKeyDown(KeyCode.A))
{
direction=3;
walking=true;
}
if(Input.GetKeyDown(KeyCode.S))
{
direction=2;
walking=true;
}
if(Input.GetKeyDown(KeyCode.D))
{
direction=1;
walking=true;
}
if(Input.GetKeyDown(KeyCode.W))
{
direction=0;
walking=true;
}
if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.D))
{
walking=false;
}


This allowed direction to stay at the last value, so the character stayed facing the same way. The idle sprite sheet is chosen based on direction, and if walking is false, it did not play the animation, just showing the first frame. It also allowed my animation controller to be very simple, with state change dependent on "direction", and animation play based on "walking".