You've got your anim.Play("idle") code set up as an else only to your test for the a-key input. I think some of the whitespace got lost in the paste (not sure though), but basically here's what I got when I re-indented the code:
void Update () {
if (Input.GetKey(KeyCode.A)) {
// Only play the clip if it is not already playing.
// Calling play will restart the clip if it is already playing.
if (!anim.IsPlaying("walkLeft")) {
anim.Play("walkLeft");
// The delegate is used here to return to the previously
// playing clip after the "hit" animation is done playing.
anim.animationCompleteDelegate = HitCompleteDelegate;
}
} else {
if (!anim.IsPlaying("idle")) {
anim.Play("idle");
anim.animationCompleteDelegate = null;
walking = false;
}
}
if (Input.GetKey(KeyCode.D)) {
if (!anim.IsPlaying("walkRight")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walkRight");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
}
}
I think this makes it more clear that the idle code is only called in one condition. While this would not be the ideal end solution, if you moved your if Input.GetKey(D) up a bit, you'd probably get the result you want for now, like so:
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)) {
// Only play the clip if it is not already playing.
// Calling play will restart the clip if it is already playing.
if (!anim.IsPlaying("walkLeft")) {
anim.Play("walkLeft");
// The delegate is used here to return to the previously
// playing clip after the "hit" animation is done playing.
anim.animationCompleteDelegate = HitCompleteDelegate;
}
} else if (Input.GetKey(KeyCode.D)) {
if (!anim.IsPlaying("walkRight")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walkRight");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
} else {
if (!anim.IsPlaying("idle")) {
anim.Play("idle");
anim.animationCompleteDelegate = null;
walking = false;
}
}
}
At some point you'll probably want to change this entirely, so that the walking animation is based on the character's actual movement (based on the speed, direction, change-of-direction, etc) but eh. This'll work for now.