The problem I’m facing now is fail to change multiple sprite mecanim state using C# script. When I move my sprite, I can successfully change it from idle to backwalk state, however it limit my changes solely to backwalk state but not all other states. I want to make it like when I press right arrow key it change to rightwalk state, while press left arrow key to change it to leftwalk state and so on. Unfortunately the if else statement that I declare is not working making my changing movement only stick to one state. I wonder what mistake I made, and how I can fix my code?
I’m a newbie to Unity and not familiar with C#. I just learn it from the official Unity tutorials series. I been searching for many related question regard of the problem but still I can’t figure out the solution. This is the first question I ask at Unity community forum, sorry if I violate any rules. And I appreciate your help sincerely.
Here I attach my code:
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
/*
Turning();
*/
Animating(h, v);
if (Input.GetKey("up"))
anim.CrossFade("BackWalk", 0f);
if (Input.GetKey("down"))
anim.CrossFade("FrontWalk", 0f);
}
enum TaoistState { Front = 0, Back = 1, Right = 2, Left = 3};
TaoistState currentState;
void Animating(float h, float v)
{
bool walkFront = h != 0f || v != 0f;
bool walkBack = h != 0f || v != 0f;
bool walkRight = h != 0f || v != 0f;
bool walkLeft = h != 0f || v != 0f;
switch (currentState)
{
case TaoistState.Front:
anim.SetBool("BackWalk", walkBack);
break;
case TaoistState.Back:
anim.SetBool("FrontWalk", walkFront);
break;
case TaoistState.Right:
anim.SetBool("RightWalk", walkRight);
break;
case TaoistState.Left:
anim.SetBool("RightWalk", walkLeft);
break;
}
}