How to prevent multiple animations playing at the same time on the same object.

So I am a beginner and I am trying to create an animation script for my 2d Character (Who is a Cowboy) using Input.GetKey() to change the direction he is looking. However when I am having a problem where when I press two trigger keys at the same time, for example “d” and “s”, it plays the animation for both “d” and “s” at the same time. Please help!!! Thanks!

public Animator anim;

// Use this for initialization
void Start()
{
    anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
    
    if (Input.GetKey("d"))
    {
        anim.Play("Cowboy_Walk_Side");
    }

    if (Input.GetKey("a"))
    {
        anim.Play("Cowboy_Walk_Side");
    }

    if (Input.GetKey("s"))

    {
        anim.Play("Cowboy_Walk_Front");
    }

    if (Input.GetKey("w"))
    {
        anim.Play("Cowboy_Walk_Back");
    }

    
}

And I have another line of code that flips my character horizontally in my character controller that’s why anim.play(“Cowboy_Walk_Side”) is used twice.

you can use state-machines, and make ‘Has exit time’ bool to true, this way you can stop animations from being overlay. And the best practice would be to use bools for transitions of animations