Teachers open the door but You must enter by yourself.
【事前学習】前回学んだ機能を再確認しておきましょう。
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にチェックをつける必要があります。
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」に変更します。
UnityにはFile/Build SettingsでPC Standalone以外のプラットフォーム版も生成する機能が備わっています。授業では割愛しますが、機会があったらやってみてください。
【事後学習】本日学んだ機能を再確認しておきましょう。
This site is powered by