public class JumpTest : MonoBehaviour
{
public float maxHeight = 3; //跳躍高度
public float maxHeightTime = 0.5f; //到最大高度的時間
private float y0; //紀錄一開始的高度
private float t; //紀錄時間
private bool jumping;
private void Start()
{
Initialize();
}
private void Update()
{
if (Input.GetMouseButton(0))
Jump();
if (Input.GetMouseButton(1))
Initialize();
if (!jumping)
return;
//y = -(3 / 0.5 ^ 2) * (x - 0.5) ^ 2 + 3
float m = -maxHeight / Mathf.Pow(maxHeightTime, 2);
float y = m * Mathf.Pow(t - maxHeightTime, 2) + maxHeight + y0;
transform.position = Vector2.up * y;
t += Time.deltaTime;
}
private void Jump()
{
jumping = true;
}
private void Initialize()
{
y0 = transform.position.y;
t = 0;
jumping = false;
}
}