通过使用 DOTween 插件实现金币两段飞行效果,第一段在物体周围随机生成指定数量的金币,第二段将金币移动到指定位置。
DoTween 是Unity的一款插件,主要用于控制物体的移动和变换。
DOTween插件https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676



public class FlyControl : MonoBehaviour {
private Vector3 endPos;
private Vector3 startPos;
private Vector3 vec1;
private Vector3 vec2;
[NonSerialized]public bool fly = true;
public GameObject coin;
//第一段位移 控制随机出现在父物体周围
public void FlyCoinOne() {
startPos = transform.position;
float randomX = Random.Range(-50, 50);
float randomY = Random.Range(-50, 50);
vec1 = startPos + new Vector3(randomX, randomY, 0);
coin.transform.DOMove(vec1, 0.3f).SetTarget(this);
}
//第二段位移 移动到指定位置
public void FlyCoinTwo(Transform targetPos) {
endPos = targetPos.position;
vec2 = endPos;
coin.transform.DOMove(vec2, 0.6f).SetTarget(this);
float timer1 = 0;
DOTween.To(() => timer1, x => timer1 = x, 1, 0.3f)
.OnStepComplete(() => { this.gameObject.GetComponent<Image>().DOFade(0, 0.3f).SetTarget(this); });
float timer = 0;
DOTween.To(() => timer, x => timer = x, 1, 0.61f)
.OnStepComplete(() => {
fly = false;
//Destroy(this.gameObject);
});
}
}
public class CoinEffect : MonoBehaviour{
[LabelText("金币预制体")] public GameObject prefab;
public Transform parent;
[LabelText("金币数量")] public int num;
[LabelText("最终目的地")] public Transform targetPos;
[LabelText("金币飞行速度")] public float time = 1;
private int childNum = 0;
public bool isEnd = false;
private List<FlyControl> ts = new List<FlyControl>();
public void StartEffect(){
for (int i = 0; i < num; i++){
GameObject go = Instantiate(prefab, parent, false);
go.transform.position = gameObject.transform.position;
FlyControl cc = go.GetComponent<FlyControl>();
if (cc != null){
cc.gameObject.SetActive(true);
ts.Add(cc);
childNum++;
}
ts[i].FlyCoinOne();
}
}
IEnumerator StartEffect1(){
yield return new WaitForSeconds(0.6f);
for (int i = 0; i <= ts.Count; i++){
this.DOKill();
yield return new WaitForSeconds(0.1f);
var rm = Random.Range(0, ts.Count);
yield return new WaitForSeconds(0.01f);
ts[rm].FlyCoinTwo(targetPos);
// ts.Remove(ts[rm]);
Debug.Log(rm);
i = 0;
}
MyLogger.PrintLog("飞行结束");
}
///
/// 金币飞行效果,在Update里面调用
///
public void CoinEffectShow(){
//第一次飞行
StartEffect();
//第二次飞行
StartCoroutine(StartEffect1());
}
private void Update(){
if (childNum == 0){
return;
}
foreach (var control in ts){
if (control.fly){
return;
}
}
isEnd = true;
if (Input.GetKeyDown(KeyCode.R)){
CoinEffectShow();
}
}
}



提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。