How to Play specifc Walk animations on WASD?

Hi guys, i am making a First person adventure game and I have created a script to play individual walking animations. When I play although, it seems to bug out, and for example, i press w, then d, then s, the a, then w, it dosn’t play the w animation. Can anyone post improved script so it can work more robust?

function Update () {

        if(Input.GetKeyDown("w")) {
         
        animation.CrossFade("run_forward");
        }
         
        if(Input.GetKeyUp("w")) {
         
        animation.CrossFade("idle");
        }
         
        if(Input.GetKeyDown("a")) {
         
        animation.CrossFade("run_left");
        }
         
        if(Input.GetKeyUp("a")) {
         
        animation.CrossFade("idle");
        }
        if(Input.GetKeyDown("d")) {
         
        animation.CrossFade("run_right");
        }
         
        if(Input.GetKeyUp("d")) {
         
        animation.CrossFade("idle");
        }
        if(Input.GetKeyDown("s")) {
         
        animation.CrossFade("run_backward");
        }
        if(Input.GetKeyUp("s")) {
         
        animation.CrossFade("idle");
         
        }

}

I think to get this working it needs to be restructured to this:

function Update() {
    if (Input.GetKeyDown(KeyCode.W)) {
        animation.CrossFade("run_forward");
    } else if (Input.GetKeyDown(KeyCode.A)) {
        animation.CrossFade("run_left");
    } else if (Input.GetKeyDown(KeyCode.D)) {
        animation.CrossFade("run_right");
    } else if (Input.GetKeyDown(KeyCode.S)) {
        animation.CrossFade("run_backward");
    } else {
        animation.CrossFade("idle");
    }
}

This way only one animation will play at a time and if a button isn’t being held down the idle animation will just play. Also it may be worth putting all that code into another function to unbloat your update function like so:

function UpdateAnimations() {
 if (Input.GetKeyDown(KeyCode.W)) {
        animation.CrossFade("run_forward");
    } else if (Input.GetKeyDown(KeyCode.A)) {
        animation.CrossFade("run_left");
    } else if (Input.GetKeyDown(KeyCode.D)) {
        animation.CrossFade("run_right");
    } else if (Input.GetKeyDown(KeyCode.S)) {
        animation.CrossFade("run_backward");
    } else {
        animation.CrossFade("idle");
    }
}

and then your Update method can look like this:

function Update() {
    UpdateAnimations();
}

The basic way is you can just create a simple to fix it…

function Update()
{
  //This is just the basic way..
  if(Input.GetKeyUp("w"))
  {
    animation.Play("Move_forward");
  }
}

something like this worked for me…
not the most elegant code.

if (Input.GetKeyDown(KeyCode.W)) {
	animation.CrossFade("Run");
} else if (Input.GetKeyDown(KeyCode.A)) {
	animation.CrossFade("Run");
} else if (Input.GetKeyDown(KeyCode.D)) {
	animation.CrossFade("Run");
} else if (Input.GetKeyDown(KeyCode.S)) {
	animation.CrossFade("Run");
} else if (Input.GetKeyDown(KeyCode.Space)) {
	animation.CrossFade("Jump");
	//WaitForSeconds(1);
	animation.CrossFadeQueued("idle");
} else if (Input.GetMouseButtonDown(0)) {
	animation.CrossFade("Attack");
	animation.CrossFadeQueued("idle");
} else if (Input.GetKeyUp("w")||Input.GetKeyUp("a")||Input.GetKeyUp("d")) {
	animation.CrossFade("idle");
}