using System.Collections.Generic; using UnityEngine; public class PlayAnimationOnTrigger : MonoBehaviour { public enum KnownAnimations { UFOFlight1 } protected Dictionary animationNames = new Dictionary(); [SerializeField] public Animator animator; // Reference to the Animator component [SerializeField] public KnownAnimations animationName = KnownAnimations.UFOFlight1; // Name of the animation to play protected void Awake() { animationNames[KnownAnimations.UFOFlight1] = "UFO group flight 1"; } private void OnTriggerEnter(Collider other) { // Check if the animator is assigned if (animator == null) { Debug.LogWarning("Animator not assigned!", this); return; } string animationNameString = animationNames[animationName]; // Check if the animation is already playing if (animator.GetCurrentAnimatorStateInfo(0).IsName(animationNameString) && animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) { return; } // Play the animation animator.Play(animationNameString, 0, 0f); Debug.Log("Playing animation: " + animationNameString); } }