Animator state machine not triggering

I’d really like to learn how to create AI boss fights using the animator state machine function.

Sadly when I try I can’t seem to trigger an animation.

Any tips on where I’m going wrong would be great.

The second ‘Punch’ animation plays once, instantly after the first Idle animation and then returns back to Idle, regardless if distance to player.


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

public class GeoPunch : StateMachineBehaviour
{
    Transform player;
    public float dist = 2;
    Rigidbody rb;

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        player = GameObject.FindWithTag("Player").transform;
        rb = animator.GetComponent<Rigidbody>();
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
       if ( Vector3.Distance(player.position, rb.position) <= dist )
       {
         animator.SetTrigger("Punch");
       }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {

    }

}

Seems to work if the rigidbody attribute ‘is kinematic’ is ticked