Teachers open the door but You must enter by yourself.

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

スペースシューター
Space Shooter

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


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

Scoring, Finishing and Building the Game
Counting points and displaying the score

操作手順

  1. Create/3D Object/Text-TextMeshPro でテキスト表示部分を生成し(TMP Importerがポップアップ表示されたらImport TMP Essentialsのボタンを押す)、Canvas/TextをCanvas/Score Textなどの名前にする。
  2. 表示範囲やフォントサイズを調整して、画面左上に適当な文字の大きさで配置する。Color を視認性の良い白などに変更する。
  3. GameControllerにGameControllerのタグをつける。
  4. GameController.csに以下のように追記。
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    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;
    
    	void Start(){
    		scoreText.text = "Score: 0";
    		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);
    		}
    	}
    	public void AddScore(int newScoreValue){
    		score += newScoreValue;
    		scoreText.text = "Score: " + score;
    	}
    }
    
  5. GameController の Inspector ウィンドウで、Score TextにCanvas/Score Text をドラッグアンドドロップ。
  6. DestroyByContact.cs に以下のように追記。
    
    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
    			);
    			GameObject.Find("/GameOver").GetComponent<MeshRenderer>().enabled=true;
    		}
    		gameController.AddScore(scoreValue);
    		Destroy(other.gameObject);
    		Destroy(gameObject);
    	}
    }
    

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

This site is powered by
Powered by MathJax