(Unity6 BRP)着弾点に放物線上に飛んでいく砲弾の作成

(サンプルコード)

using UnityEngine;

public class TargetShooter : MonoBehaviour
{
    public GameObject shellPrefab;
    public Transform shootPoint;
    public Transform target;
    private float launchAngle = 75f; // 発射角度

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        GameObject shell = Instantiate(shellPrefab, shootPoint.position, Quaternion.identity);
        Rigidbody shellRb = shell.GetComponent();

        Vector3 direction = target.position - shootPoint.position;
        float h = direction.y; // 高さの差
        direction.y = 0; // 水平方向だけに限定
        float distance = direction.magnitude;
        float angle = launchAngle * Mathf.Deg2Rad;

        // 発射速度の算出
        float velocity = Mathf.Sqrt((distance * Physics.gravity.magnitude) / Mathf.Sin(2 * angle));

        Vector3 velocityVec = direction.normalized * velocity * Mathf.Cos(angle);
        velocityVec.y = velocity * Mathf.Sin(angle);

        shellRb.linearVelocity = velocityVec;
    }
}

(実行結果)