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

30 lines
924 B
C#

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
private void OnTriggerEnter(Collider other)
{
// Check if the animator is assigned
if (animator == null)
{
Debug.LogWarning("Animator not assigned!", this);
return;
}
// Check if the animation is already playing
if (animator.GetCurrentAnimatorStateInfo(0).IsName(animationName) && 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);
}
}