using UnityEngine;
public class Parabola : MonoBehaviour {
bool start = false;
float startY;
float Timestamp;
bool action = false;
public Vector3 distPos;
public float duration = 1;
void StartTest()
{
start = false;
Timestamp = Time.unscaledTime;
action = true;
}
void Test(ref Vector3 pos)
{
if (!start) {
//시작
start = true;
startY = pos.y;
}
float elapsedTime = Time.unscaledTime - Timestamp;
float duration = this.duration * 0.5f;
float gravity = 2 * distPos.y / (duration * duration);
float velocity = Mathf.Sqrt(2 * gravity * distPos.y);
float height = startY + velocity * elapsedTime;
//var highestPoint = velocity / gravity * 2;
var highestPointTime = velocity / gravity;
if (highestPointTime < elapsedTime) {
GetComponent<Renderer>().material.color = Color.red;
}
height -= (0.5f * gravity * elapsedTime * elapsedTime);
pos.y = height;
if (elapsedTime > this.duration) {
//종료
action = false;
pos.y = startY;
GetComponent<Renderer>().material.color = Color.blue;
}
return;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) {
StartTest();
}
if (action) {
var pos = transform.position;
Test(ref pos);
transform.position = pos;
}
}
}