Run new animation only when previous one is finished

I’m new to Unity and I’m struggling with configuring my animations properly. I have attack animation which must be played if you press the spacebar once and if you hold it it should play until you release. My problem is that once you release the spacebar the animator instantly returns to the previous state. I’ve read some answer here on the Unity Q&A site but they are all concerning a script that runs the animations, I dont use any script to run mine I’m simply setting them up in the animator. How can I fix this ? The way I change the animation is by switching the value of a boolean variable like this :

        if(Input.GetKeyDown(KeyCode.Space))
        {
            IsAttacking = true;
        }
        if(Input.GetKeyUp(KeyCode.Space))
        {
            IsAttacking = false;
        }
        playerAnimator.SetBool("IsAttacking", IsAttacking);

There’s probably a problem in how you setup the transition between attacking and the other states, your code is simple and should work, but you can fix a transition using code if the settings are wrong in the editor. Share a screenshop of all the settings, you probably unhecked the “Has Exit Time” option.