Files
MRFishingMaster/Assets/_FishingMaster/Scripts/Item/Coin.cs

121 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using System.Collections;
using DG.Tweening;
public class Coin : MonoBehaviour
{
private Vector3 velocity;
private bool flying;
private Transform target;
private float flyDuration;
private float shrinkStartDistance;
private float timer;
private Vector3 startPos;
private int coinValue = 1;
[SerializeField] private float rotationDuration = 2f; // 旋转一圈的时间
[SerializeField] private RotateMode rotateMode = RotateMode.FastBeyond360; // 旋转模式
[SerializeField] private Ease easeType = Ease.Linear; // 旋转曲线
public void SetValue(int value)
{
coinValue = value;
StartRotation();
}
private void StartRotation()
{
// 获取当前旋转角度
Vector3 currentRotation = transform.eulerAngles;
// 设置目标角度Y轴增加360度
Vector3 targetRotation = new Vector3(
currentRotation.x,
currentRotation.y + 360f,
currentRotation.z
);
// 执行旋转动画
transform.DORotate(targetRotation, rotationDuration, rotateMode)
.SetEase(easeType)
.SetLoops(-1, LoopType.Restart) // -1 表示无限循环
.SetRelative(true); // 使用相对旋转
}
public void ExplodeTo(Vector3 targetPos, float force)
{
startPos = transform.position;
StartCoroutine(ExplodeRoutine(targetPos, force));
}
IEnumerator ExplodeRoutine(Vector3 targetPos, float force)
{
float t = 0;
Vector3 origin = transform.position;
while (t < 0.25f)
{
t += Time.deltaTime * force;
transform.position = Vector3.Lerp(origin, targetPos, t);
yield return null;
}
}
public void FlyTo(Transform targetTransform, float duration, float shrinkDistance)
{
target = targetTransform;
flyDuration = duration;
shrinkStartDistance = shrinkDistance;
flying = true;
timer = 0;
startPos = transform.position;
}
void Update()
{
if (!flying || target == null) return;
timer += Time.deltaTime;
float t = Mathf.Clamp01(timer / flyDuration);
t = Mathf.SmoothStep(0, 1, t);
// 🌟 流线插值(略带弯曲)
Vector3 mid = (startPos + target.position) * 0.5f + Vector3.right * 0.3f;
Vector3 pos = Bezier(startPos, mid, target.position, t);
transform.position = pos;
float dist = Vector3.Distance(pos, target.position);
// 🌟 接近目标开始缩小
if (dist < shrinkStartDistance)
{
float scale = dist / shrinkStartDistance;
transform.localScale = Vector3.one * scale;
}
if (t >= 1f)
{
Collect();
}
}
void Collect()
{
GameInit.Ins.self.AddCoin(coinValue);
Destroy(gameObject);
}
Vector3 Bezier(Vector3 a, Vector3 b, Vector3 c, float t)
{
return Vector3.Lerp(
Vector3.Lerp(a, b, t),
Vector3.Lerp(b, c, t),
t
);
}
}