(Unity)戦闘ヘリの攻撃力の実装

<機関砲>

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeliShotBullet : MonoBehaviour
{
    public GameObject bulletPrefab;
    private int count;
    public AudioClip sound;

    void Update()
    {
        if(Input.GetMouseButton(0))
        {
            count += 1;

            if(count % 10 == 0)
            {
                GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
                Rigidbody bulletRb = bullet.GetComponent<Rigidbody>();
                bulletRb.AddForce(transform.forward * 2000);
                AudioSource.PlayClipAtPoint(sound, transform.position);
                Destroy(bullet, 3.0f);
            }
        }
    }
}

<ミサイル本体に追加>

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeliMissile : MonoBehaviour
{
    private Rigidbody rb;
    public GameObject effecPrefab;
    public AudioClip sound;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        rb.velocity += transform.forward * 0.3f;
    }

    private void OnCollisionEnter(Collision collision)
    {
        Destroy(collision.gameObject);
        Destroy(gameObject);
        GameObject effect = Instantiate(effecPrefab, transform.position, Quaternion.identity);
        Destroy(effect, 1.5f);
        AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
    }
}

<ヘリ本体に追加>

(ミサイルを発射するスクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeliShotMissile : MonoBehaviour
{
    public GameObject[] missiles;
    public AudioClip sound;

    void Update()
    {
        if(Input.GetMouseButtonDown(1))
        {
            foreach(GameObject g in missiles)
            {
                g.AddComponent<Rigidbody>();
                Rigidbody rb = g.GetComponent<Rigidbody>();
                rb.useGravity = false;
                g.GetComponent<HeliMissile>().enabled = true;
                AudioSource.PlayClipAtPoint(sound, transform.position);

                Destroy(g, 20f);
            }
        }
    }
}