Animation Help (118416)

I have an animation that plays when i attack but the problem is if i press any other button midway through attacking the animations stops and it looks bad. can anyone help? my script:
#pragma strict

var swording = false;
 
function Update () {
 
    if (!swording && Input.GetButtonDown("Fire1")) {
        DoSword();
    }   
    if (Input.GetKeyDown(KeyCode.W)) {
        animation.Play("Walking") ;
    }        
    if (Input.GetKeyUp(KeyCode.W)) {	    
         animation.Play("Idle") ;	
    }    
     
    if (Input.GetKeyDown(KeyCode.Space)) {
        animation.Play("Jumping", PlayMode.StopAll) ;
    }
}   
function DoSword() {
    swording = true;
    animation.Play("attack", PlayMode.StopAll) ;
    yield WaitForSeconds (0.68) ;
    animation.Play("Idle", PlayMode.StopAll) ;
    swording = false;
}

its a bit weird because i got someone to help me with it but it turned out the code they added didnt really need to be there so theres a chunk of useless code.

That’s because the Update function is called too often.
If a split second ago you started the attack animation and before the animation ends, you press another key then nothing is stopping the Update function from executing the another valid if block condition.

You need to mix animations.
See: How to Mix Animations in Unity3D (Legacy) - YouTube

Or, check !swording in every if condition.
But this will block input till the attack animation is over and is generally not really a good idea.

Example:

if (!swording && Input.GetKeyDown(KeyCode.W)) {
    animation.Play("Walking") ;
}
if (!swording && Input.GetKeyUp(KeyCode.W)) {
    animation.Play("Idle") ;
}
if (!swording && Input.GetKeyDown(KeyCode.Space)) {
    animation.Play("Jumping", PlayMode.StopAll) ;
}