一定時間で弾を発射するスクリプト(ランダムも可)

 

f:id:sashimimayonezu:20200825215454g:plain

先日大砲で弾を発射するゲームのチュートリアルを学んだ。

sashimimayonezu.hatenablog.com

 今回はそれを利用して一定時間で弾を発射するスクリプトにしてみる。

チュートリアルではキー操作で弾を発射するスクリプトと、

一定時間で敵を生成するスクリプトがあった。

これをif文をキー入力から時間経過を条件にすればよい。

 

以下にコードを記述

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CannonController : MonoBehaviour
{
    //bulletプレハブ
    public GameObject bullet;
    //弾が生成されるポジションを保有するゲームオブジェクト
    public GameObject bulletPos;
    //弾丸のスピード
    public float speed = 1500f;
    //敵生成時間間隔
    private float interval;
    //経過時間
    private float time = 0f;
    // Start is called before the first frame update
    void Start()
    {
        //時間間隔を決定する
        interval = 0.5f;
    }
    // Update is called once per frame
    void Update()
    {
        time += Time.deltaTime;
        //スペースが押されたとき
        if (time >interval)
        {
            //ballをインスタンス化して発射
            GameObject createdBullet = Instantiate(bullet) as GameObject;
            createdBullet.transform.position = bulletPos.transform.position;
            //発射ベクトル
            Vector3 force;
            //発射の向きと速度を決定
            force = bulletPos.transform.forward * speed;
            // Rigidbodyに力を加えて発射
            createdBullet.GetComponent<Rigidbody>().AddForce(force);
            //時間リセット
            time = 0;
        }
    }
}

 

 これでbullet,bulletPosをそれぞれオブジェクトで作成してアタッチすれば、

一定時間で発射するようになった。

f:id:sashimimayonezu:20200825215454g:plain

 参考元のサイトを使えば発射時間をランダムにもできる。

https://xr-hub.com/archives/16386

後、大砲を回転してみたりする。

void Update()
{
transform.Rotate(new Vector3(0, 0.1f, 0));
}

 

f:id:sashimimayonezu:20200825222248g:plain

こうやって障害物が作れそう。

一部スクリプトをいじって発射位置をランダムでぶれるようにしてみた。

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

public class CannonController : MonoBehaviour
{
    //bulletプレハブ
    public GameObject bullet;
    //弾が生成されるポジションを保有するゲームオブジェクト
    public GameObject bulletPos;

    //弾丸のスピード
    public float speed = 1500f;

    //敵生成時間間隔
    private float interval;
    //時間間隔の最小値
    public float minTime = 0.4f;
    //時間間隔の最大値
    public float maxTime = 0.9f;
    //経過時間
    private float time = 0f;

    //X座標の最小値
    public float xMinPosition = -15f;
    //X座標の最大値
    public float xMaxPosition = 15f;

    // Start is called before the first frame update
    void Start()
    {
        //時間間隔を決定する
        interval = GetRandomTime();
    }

    // Update is called once per frame
    void Update()
    {

        time += Time.deltaTime;
        //スペースが押されたとき
        if (time >interval)
        {
            //ballをインスタンス化して発射
            GameObject createdBullet = Instantiate(bullet) as GameObject;

            //生成した弾の位置をランダムに設定する
            createdBullet.transform.position = GetRandomPosition();

            //発射ベクトル
            Vector3 force;
            //発射の向きと速度を決定
            force = bulletPos.transform.forward * speed;

           

            // Rigidbodyに力を加えて発射
            createdBullet.GetComponent<Rigidbody>().AddForce(force);
            //時間リセット
            time = 0;

            interval = GetRandomTime();
        }
    }
    //ランダムな時間を生成する関数
    private float GetRandomTime()
    {
        return Random.Range(minTime, maxTime);
    }

        //ランダムな位置を生成する関数
    private Vector3 GetRandomPosition()
    {
        //それぞれの座標をランダムに生成する
        float x = Random.Range(xMinPosition, xMaxPosition);
 
        //Vector3型のPositionを返す
        return new Vector3(x,0,0);
    }
}

後bulletが大量になるので、カメラから外れた時に消すスクリプト

BulletのPrefabsにアタッチした。

 

public class DeleteBullet : MonoBehaviour
{
    // カメラに写っていないときに呼ばれる関数
    void OnBecameInvisible()
    {
        GameObject.Destroy(this.gameObject);
    }
}

f:id:sashimimayonezu:20200826161257g:plain

今日はここまで。

https://help.hatenablog.com/entry/developer-option?_gl=1*ojnx55*_gcl_au*NzA5NzY1Mzc3LjE3MTA2NTgyODA.