2019.04.24
2021.08.12
Fabeee社員ブログ
イントロダクション
こんにちは!私は白いパンです。 またお会いしましたね。 前回は戦闘機からミサイルを発射されるようにしました。 今回は敵戦闘機を入れましょう。
今回のイメージ
では、早速始まりましょう。
1.素材準備
今回はFREEの素材を使います。 --- 参照サイト 無料3Dモデル Unity AssetStore FREEエフェクト --- 素材ダウンロード enemy.zip それでは、素材を選択してProjectビューにドラッグ&ドロップしましょう。
2.敵の出現
・敵のプレハブを作成し、enemyCsを追加しましょう。using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemyCs : MonoBehaviour { float speed; void Start() { this.speed = 0.1f + 1f * Random.value; // 素材の具合で最初から180度転回 transform.Rotate(0, 0, 180); } void Update() { // 全体座標基準で下に移動する transform.Translate(0, -this.speed, 0, Space.World); // 表示範囲を出たらDestroyする if (transform.position.y < -55.5f) { Destroy(gameObject); } } }
・画面全体コントロール用のempty Objectを作成します。
・コントロール用C#スクリプトenemyController.csを作成し、オブジェクトにドラッグ&ドロップしましょう。using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemyController : MonoBehaviour { public GameObject enemy; // Start is called before the first frame update void Start() { InvokeRepeating ("enemyArrival", 1, 1); } void enemyArrival () { Instantiate (enemy, new Vector3 (50 * Random.value - 25f, 60, 0), Quaternion.identity); } }
3.ミサイルの当たり判定
ミサイルに判定範囲を設定する。 [Add Component]→[Physics 2D]→[Box Collider 2D] [Add Component]→[Physics 2D]→[Rigidbody 2D] 敵に当たり判定の範囲を設定する。 [Add Component]→[Physics 2D]→[Box Collider 2D] 敵のIs Triggerをチェックする。 敵以外のオブジェクトに当たり判定はしたくないですね。絞る方法は幾つあるだけど。 今回は敵プレハブのtag属性を設定する。 スクリプトMissileCsに当たり処理を追加する。public class MissileCs : MonoBehaviour { ...... void OnTriggerEnter2D(Collider2D coll) { // 敵チェック if (coll.gameObject.tag == "enemy") { // 当たりの場合は、消滅する Destroy(coll.gameObject); Destroy(gameObject); } } }
ミサイルと敵に設定した判定の範囲が接触したら、処理が発火される。
4.敵から発砲
・弾のC#スクリプトを作成し、素材の弾プレハブにドラッグ&ドロップする。using System.Collections; using System.Collections.Generic; using UnityEngine; public class BulletCs : MonoBehaviour { public GameObject explosionPrefab; //爆発エフェクトのPrefab // ミサイルインスタンスを定義 private GameObject player; // Start is called before the first frame update void Start() { // プレイヤー機体オブジェクト情報を検索 player = GameObject.Find("fighter"); if (player) { // プレイヤーの位置を向けて回転をする Vector3 diff = (player.transform.position - this.transform.position); this.transform.rotation = Quaternion.FromToRotation (Vector3.up, diff); } } // Update is called once per frame void Update() { // 砲弾自身の向ける方向に前進する transform.Translate(0, 0.3f, 0, Space.Self); // 10秒後、自分を消滅 Destroy(gameObject, 10f); } // プレイヤー機体のtagを設定して、当たり処理を追加する。 void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.tag == "Player") { Destroy (coll.gameObject); Destroy (gameObject); } } }
・敵に発砲処理を追加する。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemyCs : MonoBehaviour { // 砲弾インスタンスを定義 public GameObject bullet; ... void Start() { ... // 定期的に発砲する InvokeRepeating("fire", 2f, 1f); } ... // 発砲する void fire () { // 砲弾インスタンスを作成する Instantiate (bullet, transform.position, Quaternion.Euler(0, 0, 0)); } }
5. 爆発エフェクトを追加
・爆発エフェクトをプロジェクトに導入する。 Unity AssetStore FREEエフェクト ・爆発エフェクトループ再生のチェックを外す。 ・当たり判定の処理を更新します。当たる際に、エフェクトを再生する。public class BulletCs : MonoBehaviour { public GameObject explosion; //爆発エフェクト ...... void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.tag == "Player") { // 爆発エフェクトを生成する Instantiate (explosion, coll.gameObject.transform.position, Quaternion.identity); Destroy (coll.gameObject); Destroy (gameObject); } } }
・爆発エフェクトをパラメータにドラッグ&ドロップする。
・スマホにデプロイして動作を見ましょう。
まとめ
今回は2Dの接触判定処理を主に説明しました。 如何でしょうか。 モデルではなく、モデルに設定した判定範囲が当たることで、接触の判定をおこにます。 範囲の大きさや形を変更して、あたり感を調整できます!次回は、背景とカメラーのコントロールを入れます。 お楽しみにしてください。