I am trying to make a gun recoil with an animation and in this script it only runs after the first mouse click. This is probably an easy fix I just can't seem to wrap my head around it. This is my script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shootanim : MonoBehaviour {

public Animator anim;

void Start () {
	anim = GetComponent<Animator>();
    
}

// Update is called once per frame
void Update () {
	if(Input.GetButton("Fire1")){
        
        anim.Play("shoot");
    }
        else if(Input.GetButtonUp("Fire1")){
            anim.enabled = false;
           
        }
            
    }
    }

Hi

To make your code work, I think that you only need to write anim.Stop("shoot"); at line 13 instead of anim.enabled = false;
But there is a difference between animation & animator and it’s not the appropriate way to use the animator component


It’s more useful to create an animator if the character have several animations which can bridge between them
I advise you to create an animator for your character ( simple and efficient )
You can look there : The Animator Controller - Unity Official Tutorials - YouTube

In the other case, you can use the animation component which work like you did above.