How can I make the if statement recognize a animation?

How can I make the ‘if’ statement look to see if another animation is not running?


This is the code:

    [SerializeField] private float speed;
    [SerializeField] public float jumpPower;
    private Rigidbody2D body;
    private Animator anim;

    private void Awake()
    {
        //Grab reference for rigidbody and animator from object
        body = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
     
        //Move player
        transform.position = Vector3.right * speed * Time.deltaTime + transform.position;
        if(!anim)
        {
            anim.SetTrigger("Run");
        }

        
        if(Input.GetKeyDown("space"))
        {
            body.velocity = Vector3.up * jumpPower;
            anim.SetTrigger("Jump");
        }

    }

A common way of doing this is to set a bool when the animation starts (i.e. in the code where you start the animation). Then add an Animation Event at the end of the animation timeline which calls a method that turns the bool off. You can then test for that bool in your second piece of code.

If you’re not familiar with Animation Events, they’re really useful. Just to the left of the Animation timeline there is a little button with a vertical mark and a plus sign. That adds an event at the current timeline position. You then set the properties of the event in the einspector to say which function you want to call in the scripts attached to the game object. Just one thing to nots: the method must be marked as public for the animation to see it.