Unity/C# 개발 노트

유니티 포물선이동 예제

WithChan 2023. 8. 4. 19:04
728x90
반응형
  • 포물선 이동 예제1

public class Cannon : MonoBehaviour
{
    public GameObject projectilePrefab; // 발사할 포탄의 프리팹
    public Transform firePoint; // 포탄을 발사할 위치
    public float launchPower; // 포탄을 발사할 힘

    public void Fire()
    {
        // 발사 위치에 새로운 포탄을 생성합니다.
        GameObject projectile = Instantiate(projectilePrefab, firePoint.position, Quaternion.identity);

        // 포탄의 Rigidbody 컴포넌트를 가져옵니다.
        Rigidbody rb = projectile.GetComponent<Rigidbody>();

        // 포탄이 Rigidbody 컴포넌트를 가지고 있다면
        if (rb != null)
        {
            // 발사 방향을 firePoint의 forward 방향으로 설정하고, 발사 힘을 적용합니다.
            Vector3 force = firePoint.forward * launchPower;

            // 계산된 힘을 포탄에 적용합니다.
            rb.AddForce(force, ForceMode.Impulse);
        }
    }
}

 

  • 포물선 이동 예제2

 

public class Cannon : MonoBehaviour
{
    public GameObject projectilePrefab; // 발사할 포탄의 프리팹
    public Transform firePoint; // 포탄을 발사할 위치
    public float launchAngle; // 포탄을 발사할 각도
    public float launchPower; // 포탄을 발사할 힘

    public void Fire()
    {
        // 발사 위치에 새로운 포탄을 생성합니다.
        GameObject projectile = Instantiate(projectilePrefab, firePoint.position, Quaternion.identity);

        // 포탄의 Rigidbody 컴포넌트를 가져옵니다.
        Rigidbody rb = projectile.GetComponent<Rigidbody>();

        // 포탄이 Rigidbody 컴포넌트를 가지고 있다면
        if (rb != null)
        {
            // 각도를 라디안으로 변환합니다.
            float radianAngle = launchAngle * Mathf.Deg2Rad;

            // 힘의 벡터를 계산합니다.
            Vector3 force = new Vector3(Mathf.Cos(radianAngle), Mathf.Sin(radianAngle), 0) * launchPower;

            // 계산된 힘을 포탄에 적용합니다.
            rb.AddForce(force, ForceMode.Impulse);
        }
    }
}

 

  • 포물선 이동 예제3

 

public class Cannon : MonoBehaviour
{
    public Transform startPoint; // 발사 위치
    public Transform targetPoint; // 목표 위치
    public GameObject projectilePrefab; // 발사할 객체의 프리팹
    public float firingAngle = 45.0f;
    public float gravity = 9.8f;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(SimulateProjectile());
        }
    }

    public IEnumerator SimulateProjectile()
    {
        // 발사할 객체 생성
        GameObject projectile = Instantiate(projectilePrefab, startPoint.position, Quaternion.identity) as GameObject;

        // 시작점과 목표점 사이의 거리 계산
        float target_Distance = Vector3.Distance(startPoint.position, targetPoint.position);

        // 초기 속도 계산
        float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);

        // XZ 평면에서의 속도 계산
        float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
        float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);

        // 비행 시간 계산
        float flightDuration = target_Distance / Vx;

        // 발사 방향 설정
        projectile.transform.rotation = Quaternion.LookRotation(targetPoint.position - startPoint.position);

        // 비행 시간 동안 이동
        float elapse_time = 0;
        while (elapse_time < flightDuration)
        {
            projectile.transform.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
            elapse_time += Time.deltaTime;
            yield return null;
        }

        // 발사한 객체 파괴
        Destroy(projectile);
    }
}

 

발사체 Rigidbody 에 Use Gravity 는 비활성화 해주셔야 작동됩니다!

 

 

728x90
반응형
LIST