先上公式
public static Vector3 Parabola(Vector3 start, Vector3 end, float totalTime, float curTime)
{
//var g = 9.8f;
var g = 50f;
var halfTime = totalTime / 2f;
var vec = (end - start) / totalTime;
var initVelocity = vec + new Vector3(0, g * halfTime, 0);
var acc = new Vector3(0, -g, 0);
var curPos = start + initVelocity * curTime + 1f / 2f * acc * curTime * curTime;
return curPos;
}
使用方法,这里我用的是协程
float totalTime = 0.5f;
float startTime = Time.time;
while (true)
{
if (Time.time > startTime + totalTime)
{
break;
}
var curTime = Time.time - startTime;
goGrenade.transform.position = MathTool.Parabola(startPos, targetPos, totalTime, curTime);
yield return null;
}