Files
DeltaVR/Assets/_PROJECT/Components/Portals2/Play Animation On Trigger.cs

43 lines
1.3 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class PlayAnimationOnTrigger : MonoBehaviour
{
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)
{
// 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);
}
}