(サンプルコード)
using UnityEngine;
public class TargetShooter : MonoBehaviour
{
public GameObject shellPrefab;
public Transform shootPoint;
public Transform target;
private float launchAngle = 75f;
// ★追加(タイマー)
private float count;
private float interval = 2f;
private void Update()
{
// ★追加(タイマー)
count += Time.deltaTime;
if(count > interval)
{
Shoot();
count = 0;
}
}
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;
}
}
(実行結果)