using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class TextHp : MonoBehaviour
{
private float _times = 0.5F;
private float _pointCount = 5F;
private float _distance = 0.25F;
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
DoAnim(-6,1);
}
}
//参数1:掉血值 ,2:1为向右,-1为向左
public void DoAnim(int subHp, int dir)
{
this.GetComponent<Text>().text = string.Format("-{0}", subHp);
Vector3[] pathvec = Bezier2Path(transform.position, transform.position + new Vector3(_distance * dir, 0.5F, 0), transform.position + new Vector3(_distance * 2 * dir, 0, 0));
transform.DOPath(pathvec, _times).SetEase(Ease.Linear).OnComplete(() => { Destroy(gameObject); });
}
//获取二阶贝塞尔曲线路径数组
private Vector3[] Bezier2Path(Vector3 startPos, Vector3 controlPos, Vector3 endPos)
{
Vector3[] path = new Vector3[(int)_pointCount];
for (int i = 1; i <= _pointCount; i++)
{
float t = i / _pointCount;
path[i - 1] = Bezier2(startPos, controlPos, endPos, t);
}
return path;
}
// 2阶贝塞尔曲线
public static Vector3 Bezier2(Vector3 startPos, Vector3 controlPos, Vector3 endPos, float t)
{
return (1 - t) * (1 - t) * startPos + 2 * t * (1 - t) * controlPos + t * t * endPos;
}
// 3阶贝塞尔曲线
public static Vector3 Bezier3(Vector3 startPos, Vector3 controlPos1, Vector3 controlPos2, Vector3 endPos, float t)
{
float t2 = 1 - t;
return t2 * t2 * t2 * startPos
+ 3 * t * t2 * t2 * controlPos1
+ 3 * t * t * t2 * controlPos2
+ t * t * t * endPos;
}
}
|