public void Shoot(Transform bullet, Vector3 startPos, Vector3 endPos, float g, float max_height, System.Action onComplete)
{
start_pos = startPos;
end_pos = endPos;
this.g = g;
this.max_height = max_height;
this.bullet = bullet;
this.bullet.position = start_pos;
var dh = endPos.y - startPos.y;
var mh = max_height - startPos.y;
ty = Mathf.Sqrt(2 * this.g * mh);
float a = this.g;
float b = -2 * ty;
float c = 2 * dh;
dat = (-b + Mathf.Sqrt(b * b - 4 * a * c)) / (2 * a);
tx = -(startPos.x - endPos.x) / dat;
tz = -(startPos.z - endPos.z) / dat;
this.elapsed_time = 0;
StartCoroutine(this.ShootImpl(onComplete));
}
IEnumerator ShootImpl(System.Action onComplete)
{
while (true)
{
this.elapsed_time += Time.deltaTime;
var tx = start_pos.x + this.tx * elapsed_time;
var ty = start_pos.y + this.ty * elapsed_time - 0.5f * g * elapsed_time * elapsed_time;
var tz = start_pos.z + this.tz * elapsed_time;
var tpos = new Vector3(tx, ty, tz);
bullet.transform.LookAt(tpos);
bullet.transform.position = tpos;
if (this.elapsed_time >= this.dat)
break;
yield return null;
}
onComplete();
}
private void OnCompleteThrowSimulation()
{
Debug.Log("도착");
}
포물선으로 이동하는 물체 코드인데
왜 계산하는지 모르겠는게 너무 많어.
수학 문제 같은데 누가 좀 알려주라...
물리중에 등가속도 공식이었나? 거기서 포물선에 대해 공부하고 코드를 다시 봐라. 그걸 이해 못하고 보면 이해 못하는게 당연한 코드임
놀랍도록 정직하게 그것만 적혀있다
넹 - dc App
이거 물리2 내용인거임? 어쩐지 처음 본다 싶음 - dc App
시작점과 목표점에 맞춰서 포물선 궤적을 그려주는거 같은데. 유니티에서 버전마다 실제 발사체 궤적을 다르게 계산한다고 함. 그래서 보통은 이런 방식보다 가상의 총알(안보이는)을 만들어 발사 시켜 궤적을 시간마다 좌표를 기록해서 선을 그려주는게 더 일반적인거라고 하더라. 참고해.