スペースシューター
Space Shooter
【事前学習】前回学んだ機能を再確認しておきましょう。
Boundaries, Hazards and Enemies
Creating hazards
操作手順
- 空のオブジェクトを作成し、名前を Asteroid に変更。Position(0,0,8)。
- Asteroid の子に Models/prop_asteroid_01 を追加。Transform をリセット。
- Asteroid に Rigidbody を追加し、AngularDrag:0、Use Gravity のチェックを外す。
- Asteroid に CapsuleCollider を追加し、Radius:0.5、Height:1.5。IsTrigger をチェック。
- Asteroid にアタッチする RandomRotator.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RandomRotator : MonoBehaviour { public float tumble=5; private Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); rb.angularVelocity = Random.insideUnitSphere * tumble; } }
- Asteroid にアタッチする DestroyByContact.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyByContact : MonoBehaviour { private void OnTriggerEnter(Collider other){ if (other.gameObject.name != "Boundary"){ Destroy(other.gameObject); Destroy(gameObject); } } }
Boundaries, Hazards and Enemies
Explosions
操作手順
- Asteroid に Prefabs/VFX/Explosions 類を追加。
- Asteroid にアタッチする DestroyByContact.cs にコードを追記
public class DestroyByContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; private void OnTriggerEnter(Collider other){ if (other.gameObject.name != "Boundary"){ Instantiate(explosion, transform.position, transform.rotation); if (other.tag == "Player"){ Instantiate(playerExplosion, other.transform.position, other.transform.rotation); } Destroy(other.gameObject); Destroy(gameObject); } } }
- Bolt 用に作成した Scripts/Mover.cs を Asteroid にドラッグアンドドロップしてspeedを-5に変更する(Mover.cs の流用)。
Boundaries, Hazards and Enemies
Game Controller
操作手順
- Asteroid を Assetsフォルダにドラッグアンドドロップしてプレハブ化。
- 空のオブジェクトを作成し、名前を GameController に変更。以下のGameController.cs をアタッチ
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameController : MonoBehaviour { public GameObject hazard; public Vector3 spawnValues; void Start(){ SpawnWaves(); } void SpawnWaves(){ Vector3 spawnPosition = new Vector3( Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z ); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazard, spawnPosition, spawnRotation); } }
- GameController の Inspector で、hazard に Asteroid のプレハブをドラッグアンドドロップ。SpawnValues(6,0,16)。
Boundaries, Hazards and Enemies
Spawning waves
操作手順
- GameController.cs を改造
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameController : MonoBehaviour { public GameObject hazard; public Vector3 spawnValues; public int hazardCount=10; public float spawnWait=0.5f; public float startWait=1.0f; public float waveWait=4.0f; void Start(){ StartCoroutine(SpawnWaves()); } IEnumerator SpawnWaves(){ yield return new WaitForSeconds(startWait); while (true){ for(int i=0;i<hazardCount; i++){ Vector3 spawnPosition = new Vector3( UnityEngine.Random.Range(-spawnValues.x,spawnValues.x), spawnValues.y, spawnValues.z ); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnWait); } yield return new WaitForSeconds(waveWait); } } }
- オブジェクトが残っていくのを消すための DestroyByTime.cs を作成してexplosion_asteroid などにアタッチ。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyByTime : MonoBehaviour { public float lifetime=2.0f; void Start(){ Destroy(gameObject, lifetime); } }
【事後学習】本日学んだ機能を再確認しておきましょう。