Unity 포물선 운동 구현 (C#)

티스토리 메뉴 펼치기 댓글수9

Unity3D/Problems

Unity 포물선 운동 구현 (C#)

일등하이
댓글수9

using UnityEngine;

using System.Collections;

public class ThrowSimulator : MonoBehaviour

{

    private Transform bullet;   // 포물체

    private float tx;

    private float ty;

    private float tz;

    private float v;

    public float g = 9.8f;

    private float elapsed_time;

    public float max_height;

    private float t;

    private Vector3 start_pos;

    private Vector3 end_pos;


    private float dat;  //도착점 도달 시간 

    

    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();

    }

}







높이보다 시작지점, 끝지점이 낮을경우는 동작 하지 않습니다. 


패키지 첨부 해요 

ThrowSimulation.unitypackage



맨위로