play animation on collision

I used this code but it doesen’t work.

using UnityEngine;
public class PlayAnimOnTrigger : MonoBehaviour {

    public Animator anim;
    public AudioClip sound;

 void Start () {
        anim.enabled = false;
 }

    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == ("Player"))
        {
            anim.enabled = true;
            AudioSource.PlayClipAtPoint(sound, transform.position);
        }
    }
}

Your code does’t work because it sets animator disabled on start and enables it on first player’s collision with trigger, it means that animator will play it’s start animation and only once. To play animation on every collision you can create iddle animation as starting animation and use animator trigger to transit to animation you want play calling Animator.SetTrigger function. For more info check out:

-manual: Unity - Manual: Animator Controller ,

-video tutorial: Controlling Animation - Unity Learn ,

-documentation: Unity - Scripting API: Animator.SetTrigger

I need to play the animation only one time. But when the player collide the animation won’t start.