【Unity】 结合DoTween制作抛物线效果
概要
public partial class EMath
{
public static Vector3 Parabola(Vector3 start, Vector3 end, float height, float t)
{
float Func(float x) => 4 * (-height * x * x + height * x);
var mid = Vector3.Lerp(start, end, t);
return new Vector3(mid.x, Func(t) + Mathf.Lerp(start.y, end.y, t), mid.z);
}
public static Vector2 Parabola(Vector2 start, Vector2 end, float height, float t)
{
float Func(float x) => 4 * (-height * x * x + height * x);
var mid = Vector2.Lerp(start, end, t);
return new Vector2(mid.x, Func(t) + Mathf.Lerp(start.y, end.y, t));
}
}
使用方法
public class Test : MonoBehaviour
{
public Transform start;
public Transform target;
public Transform ball;
private float t;
private void Start()
{
DOTween.To(setter: value =>
{
Debug.Log(value);
ball.position = Parabola(start.position, target.position, 10, value);
}, startValue: 0, endValue: 1, duration: 5)
.SetEase(Ease.Linear);
}
}
效果演示