Teachers open the door but You must enter by yourself.

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

スペースシューター
Space Shooter

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


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

Scoring, Finishing and Building the Game
Ending the game

操作手順

  1. Game Over と Restart 用のテキストを Canvas の子として生成、配置し、Score と同様に設定をする。
  2. GameController.cs に以下のように追記。
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    
    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;
    
    	public Text scoreText;
    	private int score = 0;
    	public Text restart;
    	public Text gameOver;
    
    	void Start(){
    		scoreText.text = "Score: 0";
    		restart.text = "";
    		gameOver.text = "";
    		StartCoroutine(SpawnWaves());
    	}
    
    	void Update(){
    		if (!string.IsNullOrEmpty(restart.text)){
    			if (Input.GetKeyDown(KeyCode.R)){
    				SceneManager.LoadScene("SampleScene");
    			}
    		}
    	}
    
    	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);
    
    			if (!string.IsNullOrEmpty(gameOver.text)){
    				restart.text = "Press 'R' for Restart";
    				break;
    			}
    		}
    	}
    	public void AddScore(int newScoreValue){
    		score += newScoreValue;
    		scoreText.text = "Score: " + score;
    	}
    	public void GameOver(){
    		gameOver.text = "Game Over !";
    	}
    }
    
  3. DestroyByContact.cs に以下のように追記。
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class DestroyByContact : MonoBehaviour
    {
    	public GameObject explosion;
    	public GameObject playerExplosion;
    
    	public int scoreValue=10;
    	private GameController gameController;
    
    	private void Start(){
    		GameObject gc = GameObject.FindWithTag("GameController");
    		if (gc != null){
    			gameController = gc.GetComponent<GameController>();
    		}else{
    			Debug.Log("Cannot find GameController Tag Object");
    		}
    	}
    	private void OnTriggerEnter(Collider other)
    	{
    		if (other.tag == "Boundary"){
    			return;
    		}
    		Instantiate(explosion, transform.position, transform.rotation);
    		if (other.tag == "Player"){
    			Instantiate(
    				playerExplosion,
    				other.transform.position,
    				other.transform.rotation
    			);
    			gameController.GameOver();
    		}
    		gameController.AddScore(scoreValue);
    		Destroy(other.gameObject);
    		Destroy(gameObject);
    	}
    }
    

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

This site is powered by Powered by MathJax