【Unity】Version3 敵をランダムに出現させショットガンで打つ(パーティクルあり)
カメラワークもショットガンに追跡させた。
環境メモ
⭐️Mac OS Mojave バージョン10.14
⭐️Unity 2018.2.15f1
実際に動かした動画はこちら↓↓
https://twitter.com/nonnonkapibara/status/1066693012036640769
Version3 敵をランダムに出現させショットガンで打つ
— non (@nonnonkapibara) November 25, 2018
(✨パーティクル✨あり)を勉強したよぉ。
(カメラワークもショットガンに追跡させた)
(●^o^●)v#Unity #VR pic.twitter.com/uYIwlVgiY2
1.「PopEnemyScript」で敵を生成するスクリプトを書く。
ヒエラルキーウィンドウに配置している「ZomBear」3体を削除する
2.ヒエラルキーウィンドウに、「Empty Object」を作成し「PopEnemyObject」に名前を変更する
3.「PopEnemyObject」に「PopEnemyScript」をコンポーネント追加する
4.「EnemyPrefab」にZomBearを配置する
5.アイコンを選択する
6.PopEnemyObjectをプレハブ化にする
7.ヒエラルキーウィンドウにある「PopEnemyObject」を削除する
8.PopEnemyControllerScriptで敵をランダムに表示するスクリプトを書く
ヒエラルキーウィンドウで「EmptyObject」を作り「PopEnemys」に名前を変更する
「PopEnemyControllerScript」をコンポーネント追加する。
PopIntervalを0.5に設定する(敵を表示する間隔時間)
9.「PopEnemyObject」をヒエラルキーウィンドウの「PopEnemys」の子オブジェクトに配置する
10.全部で5個作る
11.ヒエラルキーウィンドウで「Effects」「Particle System」で「Particle System」をGunParticleに名前を変える
12.GunParticleをプレハブ化にする。ヒエラルキーウィンドウのGunParticleを削除する
13.プレハブ化したGunParticleを「GunBarreldEnd」にDrag&Dropする
14.「PlayGun」を選択しShootScriptの
「GanPoint」に「GunBarreldEnd」をDrag&Dropする
「GunParticle」に「GunParticle」をDrag&Dropする
15.「GunParticle」を設定する
16.ヒエラルキーウィンドウで「Effects」「Particle System」で「Particle System」をHitParticlesに名前を変える
17.ヒエラルキーウィンドウの「HitParticles」をプレハブ化する。
18.HitParticlesにパーティクルを設定する
19.パーティクル衝突時にDestroyする
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();
}
}
}