Teachers open the door but You must enter by yourself.
【事前学習】前回学んだ機能を再確認しておきましょう。
PlayerControllerのスクリプトに処理を追加します。
using System.Collections;
using System.Collections.Generic;
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 System.Collections;
using System.Collections.Generic;
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以外のプラットフォーム版も生成する機能が備わっています。授業では割愛しますが、機会があったらやってみてください。
/*
空のGameObject「Main」を生成し
C# Script「Main.cs」をAddして以下を追記
PlayerにアタッチするC# Script PlayerController.cs
PickUpにアタッチするC# Script Rotator.cs
が必要
*/
public class Main : MonoBehaviour
{
GameObject player;
GameObject mainCamera;
Vector3 offset;
void Start(){
var ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
ground.name = "Ground";
ground.transform.position = Vector3.zero;
ground.transform.localScale = new Vector3(2, 1, 2);
ground.GetComponent<Renderer>().material.color=new Color32(130,130,130,255);
GameObject cube;
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "WallWest";
cube.transform.position = new Vector3(-10, 0.5f, 0);
cube.transform.localScale = new Vector3(0.5f, 1, 20.5f);
cube.GetComponent<Renderer>().material.color=new Color32(79,79,79,255);
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "WallEast";
cube.transform.position = new Vector3(10, 0.5f, 0);
cube.transform.localScale = new Vector3(0.5f, 1, 20.5f);
cube.GetComponent<Renderer>().material.color=new Color32(79,79,79,255);
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "WallNorth";
cube.transform.position = new Vector3(0, 0.5f, 10);
cube.transform.localScale = new Vector3(20.5f, 1, 0.5f);
cube.GetComponent<Renderer>().material.color=new Color32(79,79,79,255);
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "WallSouth";
cube.transform.position = new Vector3(0, 0.5f, -10);
cube.transform.localScale = new Vector3(20.5f, 1, 0.5f);
cube.GetComponent<Renderer>().material.color=new Color32(79,79,79,255);
player = GameObject.CreatePrimitive(PrimitiveType.Sphere);
player.name="Player";
player.transform.position = new Vector3(0, 0.5f, 0);;
player.GetComponent<Renderer>().material.color=new Color32(0,220,255,255);
player.AddComponent<PlayerController>();
mainCamera=GameObject.Find("Main Camera");
mainCamera.transform.position = new Vector3(0, 10, -10);
mainCamera.transform.rotation=Quaternion.Euler(45,0,0);
offset = mainCamera.transform.position - player.transform.position;
for (var i = 0; i < 12; i++){
var pu = GameObject.CreatePrimitive(PrimitiveType.Cube);
pu.name = "PickUp"+i.ToString();
pu.transform.position = new Vector3(
Mathf.Cos(Mathf.PI*i/6)*5,
0.5f,
Mathf.Sin(Mathf.PI*i/6)*5
);
pu.AddComponent<Rotator>();
}
}
void Update(){
mainCamera.transform.position = player.transform.position + offset;
}
}
//PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed = 10;
Rigidbody rb;
float movementX;
float movementY;
//点数表示用(HierarchyにUI/Text-TextMeshProで要生成)
int count = 0;
public TextMeshProUGUI countText;
public TextMeshProUGUI winText;
void Awake(){ //Start()よりも前に実行される
var inputActionAsset
= ScriptableObject.CreateInstance();
var map = inputActionAsset.AddActionMap("Player");
var action = map.AddAction(
"Move",
InputActionType.Value
);
action.AddCompositeBinding("2DVector")
.With("Up", "/upArrow")
.With("Down", "/downArrow")
.With("Left", "/leftArrow")
.With("Right", "/rightArrow");
action.performed += OnMove;
action.Enable();
}
void Start(){
rb = gameObject.AddComponent();
countText=GameObject.Find("CountText").GetComponent();
winText=GameObject.Find("WinText").GetComponent();
countText.text = "count: 0";
winText.text="";
winText.fontSize=48;
}
void OnMove(InputAction.CallbackContext c){
Vector2 movementVector = c.ReadValue();
movementX = movementVector.x;
movementY = movementVector.y;
}
void FixedUpdate(){
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other){
if(other.gameObject.name.Substring(0,3)=="Pic"){
other.gameObject.SetActive(false);
countText.text = "count: " + (++count).ToString();
if (count >= 12){
winText.text = "You Win!";
}
}
}
}
//Rotator.cs
public class Rotator : MonoBehaviour
{
void Start(){
transform.rotation=Quaternion.Euler(45,45,45);
transform.localScale=new Vector3(0.5f,0.5f,0.5f);
GetComponent<Renderer>().material.color=new Color32(255,200,0,255);
GetComponent<BoxCollider>().isTrigger=true;
}
void Update(){
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
}
【事後学習】本日学んだ機能を再確認しておきましょう。
This site is powered by