使用的工具:dotween
效果展示:
金币飞行
1.游戏对象
CoinParent是起点,TargetPos是目标终点,金币从CoinParent炸开并飞向终点。
2.具体内容
文章来源:https://www.toymoban.com/news/detail-745708.html
3.具体代码
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FlyControl : MonoBehaviour
{
private Vector3 endpos;
private Vector3 startpos;
private Vector3 vec1;
private Vector3 vec2;
public GameObject coin;
//第一段位移
public void FlyCoinOne()
{
startpos = transform.position;
float random_x, random_y;
random_x = Random.Range(-200, 250);
random_y = Random.Range(50, 250);
vec1 = startpos + new Vector3(random_x, random_y, 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;
Tween t1 = DOTween.To(() => timer1, x => timer1 = x, 1, 0.3f)
.OnStepComplete(() =>
{
this.gameObject.GetComponent<Image>().DOFade(0, 0.3f).SetTarget(this);
});
float timer = 0;
Tween t = DOTween.To(() => timer, x => timer = x, 1, 0.61f)
.OnStepComplete(() =>
{
Destroy(this.gameObject);
});
}
}
文章来源地址https://www.toymoban.com/news/detail-745708.html
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Accessibility;
using UnityEngine.UI;
public class CoinEffect : MonoBehaviour
{
[Header("金币预制体")]
public GameObject Coin;
[Header("金币数量")]
public int num;
[Header("最终目的地")]
public Transform targetPos;
[Header("金币飞行速度")]
public float time = 1;
private int childnum = 0;
private List<FlyControl> ts = new List<FlyControl>();
public void StartEffect()
{
for (int i = 0; i < num; i++)
{
GameObject go = Instantiate(Coin, Vector3.zero, Quaternion.identity) as GameObject;
go.transform.SetParent(transform);
go.transform.localPosition = new Vector3(0, 0, 0);
FlyControl cc = go.GetComponent<FlyControl>();
if (cc != null)
{
ts.Add(cc);
}
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;
}
}
/// <summary>
/// 金币飞行效果,在Update里面调用
/// </summary>
public void CoinEffectShow()
{
//第一次飞行
StartEffect();
//第二次飞行
StartCoroutine(StartEffect1());
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
CoinEffectShow();
}
}
}
到了这里,关于Unity——金币飞行效果的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!