70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace Minigames.BirdPooper
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Listens to BirdPlayerController flap events and triggers wing flap animation.
|
|||
|
|
/// Uses Animator with a trigger parameter to play flap animation from idle state.
|
|||
|
|
/// </summary>
|
|||
|
|
public class BirdFlapAnimator : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[SerializeField] private Animator animator;
|
|||
|
|
[Tooltip("Name of the trigger parameter in the Animator")]
|
|||
|
|
[SerializeField] private string flapTriggerName = "Flap";
|
|||
|
|
|
|||
|
|
private BirdPlayerController birdController;
|
|||
|
|
|
|||
|
|
void Awake()
|
|||
|
|
{
|
|||
|
|
// Auto-assign animator component if not set
|
|||
|
|
if (animator == null)
|
|||
|
|
{
|
|||
|
|
animator = GetComponent<Animator>();
|
|||
|
|
if (animator == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogError("[BirdFlapAnimator] No Animator component found! Please add an Animator component or assign one in the Inspector.");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Find parent BirdPlayerController
|
|||
|
|
birdController = GetComponentInParent<BirdPlayerController>();
|
|||
|
|
if (birdController == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogError("[BirdFlapAnimator] No BirdPlayerController found in parent! This component must be a child of the bird GameObject.");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OnEnable()
|
|||
|
|
{
|
|||
|
|
// Subscribe to flap event
|
|||
|
|
if (birdController != null && birdController.OnFlap != null)
|
|||
|
|
{
|
|||
|
|
birdController.OnFlap.AddListener(OnBirdFlapped);
|
|||
|
|
Debug.Log("[BirdFlapAnimator] Subscribed to OnFlap event");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OnDisable()
|
|||
|
|
{
|
|||
|
|
// Unsubscribe from flap event
|
|||
|
|
if (birdController != null && birdController.OnFlap != null)
|
|||
|
|
{
|
|||
|
|
birdController.OnFlap.RemoveListener(OnBirdFlapped);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Called when the bird flaps. Triggers the flap animation.
|
|||
|
|
/// </summary>
|
|||
|
|
private void OnBirdFlapped()
|
|||
|
|
{
|
|||
|
|
if (animator == null) return;
|
|||
|
|
|
|||
|
|
// Trigger the flap animation
|
|||
|
|
animator.SetTrigger(flapTriggerName);
|
|||
|
|
Debug.Log($"[BirdFlapAnimator] Triggered animation: {flapTriggerName}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|