Teachers open the door but You must enter by yourself.

Open Media Lab.
オープンメディアラボ

スペースシューター
Space Shooter

「Space Shooter」公式チュートリアル


【事前学習】前回学んだ機能を再確認しておきましょう。

Boundaries, Hazards and Enemies
Creating hazards

操作手順

  1. 空のオブジェクトを作成し、名前を Asteroid に変更。Position(0,0,8)。
  2. Asteroid の子に Models/prop_asteroid_01 を追加。Transform をリセット。
  3. Asteroid に Rigidbody を追加し、AngularDrag:0、Use Gravity のチェックを外す。
  4. Asteroid に CapsuleCollider を追加し、Radius:0.5、Height:1.5。IsTrigger をチェック。
  5. 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;
    	}
    }
    
  6. 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

操作手順

  1. Asteroid に Prefabs/VFX/Explosions 類を追加。
  2. 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);
    		}
    	}
    }
    
  3. Bolt 用に作成した Scripts/Mover.cs を Asteroid にドラッグアンドドロップしてspeedを-5に変更する(Mover.cs の流用)。

Boundaries, Hazards and Enemies
Game Controller

操作手順

  1. Asteroid を Assetsフォルダにドラッグアンドドロップしてプレハブ化。
  2. 空のオブジェクトを作成し、名前を 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);
    	}
    }
    
  3. GameController の Inspector で、hazard に Asteroid のプレハブをドラッグアンドドロップ。SpawnValues(6,0,16)。

Boundaries, Hazards and Enemies
Spawning waves

操作手順

  1. 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);
    		}
    	}
    }
    
  2. オブジェクトが残っていくのを消すための 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);
    	}
    }
    

【事後学習】本日学んだ機能を再確認しておきましょう。

This site is powered by Powered by MathJax