Character still walking when no key is already pressed

I have the following animation controller:

As you can see, there are 4 different walking animations and 4 corresponding idle animations.

There are also transition conditions between them:

any state -> walkdown -- DirY < -0.1
any state -> walkleft -- DirX < -0.1
any state -> walkright -- DirX > 0.1
any state -> walkup -- DirY > 0.1

walkdown -> idledown -- DirY < 0.1 && DirY > -0.1
walkleft -> idleleft -- DirX > -0.1 && DirX < 0.1
walkright -> idleright -- DirX < 0.1
walkup -> idleup -- DirY < 0.1

I also wrote the following code for the character animation:

void FixedUpdate() {
    float xAxis = Input.GetAxisRaw("Horizontal");
    float yAxis = Input.GetAxisRaw("Vertical");

    GetComponent<Rigidbody2D>().velocity = new Vector2(xAxis, yAxis) * Speed;

    GetComponent<Animator>().SetFloat("DirX", xAxis);
    GetComponent<Animator>().SetFloat("DirY", yAxis);
}

It works but I have the following questions:

  • Is it the appropriate way to make transitions between such states? I mean, DirY < 0.1 && DirY > -0.1 and DirX > -0.1 && DirX < 0.1 looks quite rude but hey, is it ok?
  • I noticed that sometimes the character still walking when no key is already pressed. I think that’s because of the FixedUpdate method nature – it is called every n milliseconds. Am I right? Is there any workaround to change animation immediately? I already unchecked the Has Exit Time checkbox in Inspector.

Movement really makes more sense inside Update(), FixedUpdate() is best saved for physics, though it’s timing has nothing to do with it. Also you’ll probably want to try transform.Translate and doing * Time.DeltaTime or Time.SmoothDeltaTime.

As for the character still moving, consider having the idle states as the defaults then moving into the moving anim by the Speed variable without an exit time, then back to idle when the speed paramater changes. If you want the anim to stop immediately as well you’ll need to untick the Has Exit Time for those as well.