Teachers open the door but You must enter by yourself.

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

玉転がし
Roll a Ball

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

Detecting Collisions with Collectibles

PlayerControllerのスクリプトに処理を追加します。


using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
	public float speed = 10f;

	Rigidbody rb;
	Vector3 movement;

	void Start(){
		rb = GetComponent<Rigidbody>();
	}

	void OnMove(InputValue movementValue){
		var movement2D= movementValue.Get<Vector2>();
		movement=new Vector3(movement2D.x, 0f, movement2D.y);
	}

	void FixedUpdate(){
		rb.AddForce(movement * speed);
	}

	//collectableとの衝突が検出されると自動的に実行される
	void OnTriggerEnter(Collider other){
		var o=other.gameObject;
		if(o.CompareTag("PickUp")){
			o.SetActive(false);
		}
	}
}

※ Pick Up に "PickUp"のタグを(生成してから)付ける必要があります。
※ Pick Up のBoxColliderのIsTriggerにチェックをつける必要があります。

Displaying Score and Text

  1. HierarcyウインドウでUI/Text-TextMesh Proを生成し、Import TMP Essentialsのボタンを押し、TMP Importerウィンドウを閉じます。
  2. HierarcyウインドウでCanvasの子のText(TMP)をCount Textに名前を変更します。
  3. Hierarcyウインドウで再度UI/Text-TextMesh Proを生成しCanvasの子の新しく生成されたText(TMP)をWin Textに名前を変更します。
  4. 上記のテキストを表示するためのオブジェクトは、後で生成されるPlayerController.csのフォームにアタッチする必要があります。
  5. PlayerControllerのスクリプトに処理を追加します。
  6. 
    using UnityEngine;
    using UnityEngine.InputSystem;
    using TMPro;
    
    public class PlayerController : MonoBehaviour
    {
    
    	public float speed = 10f;
    
    	Rigidbody rb;
    	Vector3 movement;
    	
    	//点数表示用
    	private int count = 0;
    	public TextMeshProUGUI countText;
    	public TextMeshProUGUI winText;
    
    	void Start(){
    		rb = GetComponent<Rigidbody>();
    		countText.text = "count: 0";
    		winText.text="";
    	}
    
    	void OnMove(InputValue movementValue){
    		var movement2D= movementValue.Get();
    		movement=new Vector3(movement2D.x, 0f, movement2D.y);
    	}
    	
    	void FixedUpdate(){
    		rb.AddForce(movement * speed);
    	}
    
    	void OnTriggerEnter(Collider other){
    		if(other.gameObject.CompareTag("PickUp"))
    		{
    			other.gameObject.SetActive(false);
    			countText.text = "count: " + (++count).ToString();
    			if (count >= 12)
    			{
    				winText.text = "You Win!";
    			}
    		}
    	}
    }
    

※ TMProの導入によってInput関係のエラーが出る場合は、Edit/Project Settings/Player/Other Settings/Actve Input Handring を「Both」に変更します。

Building the Game

UnityにはFile/Build SettingsでPC Standalone以外のプラットフォーム版も生成する機能が備わっています。授業では割愛しますが、機会があったらやってみてください。

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

This site is powered by
Powered by MathJax