2025-07-15 22:16:23 +05:00

67 lines
2.1 KiB
C#

using UnityEngine;
using DG.Tweening;
public class ButterflyZibu : MonoBehaviour
{
public Animator animator;
private Vector3 jumpTarget;
private ButterflyTarget targetButterfly;
public float jumpPower = 1f;
public float jumpDuration = 0.6f;
public float landingDuration = 0.3f;
public void JumpTo(Vector3 target, ButterflyTarget butterfly)
{
targetButterfly = butterfly;
// Only move X and Y, keep Z constant
Vector3 targetPos = new Vector3(target.x, target.y, transform.position.z);
// Trigger visual jump animation
animator.SetTrigger("Jump");
// Jump to target position
transform.DOJump(targetPos+Vector3.down*0.75f, jumpPower, 1, jumpDuration)
.SetEase(Ease.OutQuad)
.OnComplete(() =>
{
// After reaching berry, notify catch
if (targetButterfly != null)
{
targetButterfly.OnCaught();
targetButterfly = null;
}
// Smooth landing to ground level (y = 0)
Vector3 landingPos = new Vector3(transform.position.x, 0f, transform.position.z);
transform.DOMoveY(landingPos.y, landingDuration)
.SetEase(Ease.InQuad);
});
}
private System.Collections.IEnumerator CallJumpPeakAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
OnJumpPeak(); // Call the same logic as animation event
}
private System.Collections.IEnumerator CallJumpEndAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
OnJumpEnd();
}
// Called from animation event at jump peak
public void OnJumpPeak()
{
transform.position = jumpTarget;
if (targetButterfly != null)
{
targetButterfly.OnCaught();
targetButterfly = null;
}
}
// Optional: Reset to ground at end of animation
public void OnJumpEnd()
{
transform.position = new Vector3(transform.position.x, 0f, transform.position.z);
}
}