how to get walking animation to stop playing

I am trying to get this character to stop playing its animation when you release the w, a, s, or d key but now when I am switching from one key to another the animation won’t play.
here is the script

void Update()
{
    if(Input.GetKeyDown("w"))
    {          
        anim.Play ("BasicMotions@Walk01"); 
    }

     if (Input.GetKeyUp("w"))
     {
         
         anim.Play ("Idle ");
        
     }

     if (Input.GetKeyUp("a"))
     {
         
         anim.Play ("Idle ");
        
     }
     if (Input.GetKeyUp("s"))
     {
         
         anim.Play ("Idle ");
        
     }
     if (Input.GetKeyUp("d"))
     {
         
         anim.Play ("Idle ");
        
     }
    
    if(Input.GetKeyDown("a"))
    {     
        anim.Play ("BasicMotions@Walk01");           
    }

   
     if(Input.GetKeyDown("d"))
    {          
        anim.Play ("BasicMotions@Walk01");           
    }

    
      if(Input.GetKeyDown("s"))
    {      
             
        anim.Play ("BasicMotions@Walk01");           
    }
}

}
`
can I please have some help, thanks!

Try replacing your code with below code
@Diamondmeh000

 anim.Play ("AnimationName", -1, 0);

This should work

if(Input.GetKey("w") ||Input.GetKey("a") ||Input.GetKey("s") ||Input.GetKey("d"))
{
    anim.Play("BasicMotions@Walk01");
}
else
{
    anim.Play("Idle");
}

if there is no w,a,s,d input , your character will play idle animation and as you hold down these keys your character will play walk animation

Use this

private Animator anim;

private void Start (){
        // Gets the Animator Component in the gameObject that have this script
        anim = GetComponent<Animator>();
}

private void Update (){
         // Gets the Input of the w,a,s,d/up,left,right,down arrows
         Vector2 moveInput = new Vector2 (Input.GetAxisRaw(Horizontal),. 
         (Input.GetAxisRaw(Vertical);
         
         // Make a float parameter called "Speed" In the Animator Component
         anim.SetFloat("Speed", moveInput.magnitude);

        // If you do not know how to use the Animator Component post a 
        comment
}

I’ve tried some new code which is this void Update() { if(Input.GetKeyDown("w") ||Input.GetKeyDown("a") ||Input.GetKeyDown("s") ||Input.GetKeyDown("d")) { anim.Play("BasicMotions@Walk01"); } if(Input.GetKeyUp("w") && Input.GetKeyUp("a") && Input.GetKeyUp("s") && Input.GetKeyUp("d")) { anim.Play("Idle"); } if(Input.GetKeyUp("w") ||Input.GetKeyUp("a") ||Input.GetKeyUp("s") ||Input.GetKeyUp("d")) { anim.Play("Idle"); } }
but there is still a problem, the character keeps zapping to random positions when I press w, a, s, or d I think its a problem with my movement code here it is `public class ThirdPersonScript : MonoBehaviour {
public Transform cam;

public CharacterController controller;

public float speed = 6f;

public float turnSmoothTime = 0.15f;
float turnSmoothVelocity;

void Start ()

{
Cursor.visible = false;
Screen.lockCursor = true;
}

void Update()
{
    float horizontal = Input.GetAxisRaw("Horizontal");
    float vertical = Input.GetAxisRaw("Vertical");
    Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

    if(direction.magnitude >= 0.1f)
    {

        float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
        transform.rotation = Quaternion.Euler(0f, angle, 0f);

        Vector3 movedirection = Quaternion.Euler(0f, targetAngle, 0f) *Vector3.forward;
        controller.Move(movedirection.normalized * speed * Time.deltaTime);
    }

}

}
`

this answer is not really an answer I just needed the code to be formatted properly. I realised that I did type Input.GetKeyDown instead of Input.GetKey and that fixed the problem but now my movement script is not working, when I press W, a, s, or d it zaps to a random position here it is

public class ThirdPersonScript : MonoBehaviour  {

public Transform cam;

public CharacterController controller;

public float speed = 6f;

public float turnSmoothTime = 0.15f;
float turnSmoothVelocity;

void Start ()

{
Cursor.visible = false;
Screen.lockCursor = true;
}

void Update()
{
    float horizontal = Input.GetAxisRaw("Horizontal");
    float vertical = Input.GetAxisRaw("Vertical");
    Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

    if(direction.magnitude >= 0.1f)
    {

        float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
        transform.rotation = Quaternion.Euler(0f, angle, 0f);

        Vector3 movedirection = Quaternion.Euler(0f, targetAngle, 0f) *Vector3.forward;
        controller.Move(movedirection.normalized * speed * Time.deltaTime);
    }

}

}
`
can I have some help please because this is extremely annoying
any help would be appreciated