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
andDirX > -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 theHas Exit Time
checkbox in Inspector.