かぴばらさんの覚書ブログ (nonkapibara 自分メモ)

かぴばらさんの覚書ブログ (nonkapibara 自分メモ)

Unity勉強中です。 AR、VR、エンターテイメント全般、ワクワクする事が大好き♪♪ O(≧∇≦)O イエイ!!

【Unity】Version3 敵をランダムに出現させショットガンで打つ(パーティクルあり)

カメラワークもショットガンに追跡させた。

環境メモ
⭐️Mac OS Mojave バージョン10.14
⭐️Unity 2018.2.15f1

 

実際に動かした動画はこちら↓↓

https://twitter.com/nonnonkapibara/status/1066693012036640769

 

1.PopEnemyScript」で敵を生成するスクリプトを書く。

ヒエラルキーウィンドウに配置している「ZomBear」3体を削除する

f:id:nonkapibara:20181125230311p:plain

2.ヒエラルキーウィンドウに、「Empty Object」を作成し「PopEnemyObject」に名前を変更する

f:id:nonkapibara:20181125230351p:plain

3.PopEnemyObject」に「PopEnemyScript」をコンポーネント追加する

f:id:nonkapibara:20181125230600p:plain

4.EnemyPrefab」にZomBearを配置する

f:id:nonkapibara:20181125230624p:plain

5.アイコンを選択する

f:id:nonkapibara:20181125230650p:plain

6.PopEnemyObjectをプレハブ化にする

f:id:nonkapibara:20181125230711p:plain

7.ヒエラルキーウィンドウにある「PopEnemyObject」を削除する

f:id:nonkapibara:20181125230732p:plain

8.PopEnemyControllerScriptで敵をランダムに表示するスクリプトを書く

ヒエラルキーウィンドウで「EmptyObject」を作り「PopEnemys」に名前を変更する

PopEnemyControllerScript」をコンポーネント追加する。

PopIntervalを0.5に設定する(敵を表示する間隔時間)

f:id:nonkapibara:20181125230801p:plain

 

f:id:nonkapibara:20181125230850p:plain

 

f:id:nonkapibara:20181125230916p:plain

9.PopEnemyObject」をヒエラルキーウィンドウの「PopEnemys」の子オブジェクトに配置する

 

f:id:nonkapibara:20181125230939p:plain

 

f:id:nonkapibara:20181125231002p:plain

10.全部で5個作る

f:id:nonkapibara:20181125231033p:plain

11.ヒエラルキーウィンドウで「Effects」「Particle System」で「Particle System」をGunParticleに名前を変える

f:id:nonkapibara:20181125231059p:plain

 

12.GunParticleをプレハブ化にする。ヒエラルキーウィンドウのGunParticleを削除する

f:id:nonkapibara:20181125231122p:plain

 

13.プレハブ化したGunParticleを「GunBarreldEnd」にDragDropする

f:id:nonkapibara:20181125231147p:plain

 

14.PlayGun」を選択しShootScript

GanPoint」に「GunBarreldEnd」をDragDropする

GunParticle」に「GunParticle」をDragDropする

f:id:nonkapibara:20181125231218p:plain

15.GunParticle」を設定する

f:id:nonkapibara:20181125231251p:plain

f:id:nonkapibara:20181125231311p:plain

16.ヒエラルキーウィンドウで「Effects」「Particle System」で「Particle System」をHitParticlesに名前を変える

f:id:nonkapibara:20181125231329p:plain

17.ヒエラルキーウィンドウの「HitParticles」をプレハブ化する。

f:id:nonkapibara:20181125231351p:plain

18.HitParticlesにパーティクルを設定する

f:id:nonkapibara:20181125231417p:plain

19.パーティクル衝突時にDestroyする

f:id:nonkapibara:20181125231449p:plain

 

 

Particle Systemの詳細は以下のUnityマニュアルを参照しました。

Particle System メインモジュール - Unity マニュアル

 

 

■BulletScript
  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Prefabsの「bullet」
 */
[RequireComponent(typeof(Rigidbody))]
public class BulletScript : MonoBehaviour {
#pragma warning disable 0649
    //RequireComponentは、アタッチし忘れを防ぐ。ここではRigidbodyは必須。
    // 球のスピード
    [SerializeField] float spped;
    // 
    [SerializeField] ParticleSystem hitParticlePrefab;

    void Start () {
        // 速度
        var velocity = spped * transform.forward;
        var rigid = GetComponent<Rigidbody>();
        // AddForceはrigidbodyへの継続的な力を追加
        // VelocityChangはrigidbodyに瞬時に速度変化を追加
        rigid.AddForce(velocity, ForceMode.VelocityChange);
    }

    // トリガーに入った時
    private void OnTriggerEnter(Collider other)
    {
        // ÉnemyScript.csを呼んで球が敵に当たると敵がDestroyする
        other.SendMessage("OnHitDestroyStart");
        Vector3 pos = transform.position;
        pos.y = 2.1f;
        transform.position = pos;
        // 敵と球の衝突
        Instantiate(hitParticlePrefab, transform.position, transform.rotation);
        Destroy(gameObject);
    }
    
}

 

 

■DestroyScript 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
 * Prefabsの「bullet」と「HitParticles」
 */
public class DestroyScript : MonoBehaviour {
#pragma warning disable 0649
    [SerializeField] float life;
    void Start () {
        // 球のゲームオブジェクトを破棄する
        Destroy(gameObject, life);
    }
}

 

■EnemyScript

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

/*
 * Prefabsの「ZomBear」
 */
public class EnemyScript : MonoBehaviour {
    /* 
     *BulletScript.csから呼ばれる。球が敵に当たると敵がDestroyする
     */
    void OnHitDestroyStart() 
    {
        Destroy(gameObject);
    }
}

 

 

■PopEnemyControllerScript

 

 

 

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

public class PopEnemyControllerScript : MonoBehaviour {
#pragma warning disable 0649
    // 敵の出現間隔
    [SerializeField] float popInterval;
    // 敵の出現リスト
    PopEnemyScript[] popEnemyList;
    float timer = 0f;

    void Start () {
        // 敵の出現リストを取得する
        popEnemyList = GetComponentsInChildren<PopEnemyScript>();
    }
    
    // Update is called once per frame
    void Update () {
        // タイマーを更新する
        timer += Time.deltaTime;

        // 出現間隔
        if (timer > popInterval) 
        {
            // ランダムに敵を出現させる
            var index = Random.Range(0, popEnemyList.Length);
            popEnemyList[index].PopEnemyStart();
            // タイマーリセットする
            timer = 0;
        }
    }
}

 

 

 

■PopEnemyScript

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

// 敵をランダムに出現させる
public class PopEnemyScript : MonoBehaviour
{
    // 敵のプレハブ
    [SerializeField] EnemyScript enemyPrefab = null;

    EnemyScript enemy = null;

    public void PopEnemyStart()
    {
        if (enemy == null)
        {
            // コピーしたいオブジェクト、
            // 敵の生成
            Vector3 pos = transform.position;
            pos.y = 0f;
            transform.position = pos;
            enemy = Instantiate(enemyPrefab, transform.position, transform.rotation);
        }

    }
}

 

 

■ShooterScript

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

public class ShooterScript : MonoBehaviour {
#pragma warning disable 0649
    // 銃弾
    [SerializeField] GameObject bulletPrefab;
    // 銃口
    [SerializeField] Transform ganPoint;
    // 銃弾発射時のパーティクル
    [SerializeField] ParticleSystem gunParticle;
    
    // Update is called once per frame
    void Update () 
    {
        // Spaceキーで球を発射する
        if (Input.GetKeyDown("space"))
        {
            Createbullet();
        }
    }

    private void Createbullet()
    {
        // 銃弾を生成する
        if (bulletPrefab != null && ganPoint != null) {
            Instantiate(bulletPrefab, ganPoint.position, ganPoint.rotation);
            // 発射のパーティクル再生する
            gunParticle.Play();
        }
    }
}