Animation player refactoring. Also, if the animation is already playing, do not log. Might be dangerous, but was a bit spam for now.

This commit is contained in:
2026-02-02 19:35:01 +02:00
parent 89b9b5bd7a
commit 768ed39abe

View File

@@ -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<KnownAnimations, string> animationNames = new Dictionary<KnownAnimations, string>();
[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);
}
}