From 768ed39abe6c763fb2157c7ba0bf8bf6f0acbda0 Mon Sep 17 00:00:00 2001 From: jee7 Date: Mon, 2 Feb 2026 19:35:01 +0200 Subject: [PATCH] Animation player refactoring. Also, if the animation is already playing, do not log. Might be dangerous, but was a bit spam for now. --- .../Portals2/Play Animation On Trigger.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Assets/_PROJECT/Components/Portals2/Play Animation On Trigger.cs b/Assets/_PROJECT/Components/Portals2/Play Animation On Trigger.cs index d26f528a..1a359c80 100644 --- a/Assets/_PROJECT/Components/Portals2/Play Animation On Trigger.cs +++ b/Assets/_PROJECT/Components/Portals2/Play Animation On Trigger.cs @@ -1,10 +1,22 @@ +using System.Collections.Generic; using UnityEngine; public class PlayAnimationOnTrigger : MonoBehaviour { - [SerializeField] public Animator animator; // Reference to the Animator component - [SerializeField] public string animationName = "YourAnimation"; // Name of the animation to play + 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) { @@ -15,15 +27,16 @@ public class PlayAnimationOnTrigger : MonoBehaviour return; } + string animationNameString = animationNames[animationName]; + // Check if the animation is already playing - if (animator.GetCurrentAnimatorStateInfo(0).IsName(animationName) && animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) + if (animator.GetCurrentAnimatorStateInfo(0).IsName(animationNameString) && animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) { - Debug.Log("Animation is already playing."); return; } // Play the animation - animator.Play(animationName, 0, 0f); - Debug.Log("Playing animation: " + animationName); + animator.Play(animationNameString, 0, 0f); + Debug.Log("Playing animation: " + animationNameString); } }