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?
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");
}
}