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:
@@ -1,10 +1,22 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class PlayAnimationOnTrigger : MonoBehaviour
|
public class PlayAnimationOnTrigger : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] public Animator animator; // Reference to the Animator component
|
public enum KnownAnimations
|
||||||
[SerializeField] public string animationName = "YourAnimation"; // Name of the animation to play
|
{
|
||||||
|
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)
|
private void OnTriggerEnter(Collider other)
|
||||||
{
|
{
|
||||||
@@ -15,15 +27,16 @@ public class PlayAnimationOnTrigger : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string animationNameString = animationNames[animationName];
|
||||||
|
|
||||||
// Check if the animation is already playing
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play the animation
|
// Play the animation
|
||||||
animator.Play(animationName, 0, 0f);
|
animator.Play(animationNameString, 0, 0f);
|
||||||
Debug.Log("Playing animation: " + animationName);
|
Debug.Log("Playing animation: " + animationNameString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user