128 lines
3.3 KiB
C#
128 lines
3.3 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using DG.Tweening;
|
||
using UnityEngine;
|
||
|
||
public enum ItemType
|
||
{
|
||
RedEnvelope,//红包
|
||
Ingot,//元宝
|
||
}
|
||
public class Item : MonoBehaviour
|
||
{
|
||
public ItemType itemType;
|
||
public Collider collider;
|
||
|
||
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; // 旋转曲线
|
||
|
||
private void Start()
|
||
{
|
||
switch (itemType)
|
||
{
|
||
case ItemType.RedEnvelope:
|
||
coinValue = 100;
|
||
break;
|
||
case ItemType.Ingot:
|
||
coinValue = 1000;
|
||
break;
|
||
}
|
||
transform.position+=Vector3.up*5;
|
||
collider = GetComponent<Collider>();
|
||
collider.enabled = false;
|
||
transform.DOMoveY(0, 3f).OnComplete(() =>
|
||
{
|
||
startPos = transform.position;
|
||
collider.enabled = true;
|
||
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); // 使用相对旋转
|
||
}
|
||
|
||
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
|
||
);
|
||
}
|
||
|
||
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
if (other.tag == "Player")
|
||
{
|
||
flying = true;
|
||
target = GameInit.Ins.self.RightHand;
|
||
GameManager.Ins.PlaySound2D("拾取音效");
|
||
}
|
||
}
|
||
}
|