# 簡易ゲームの作成
- [概要や環境設定について](https://hackmd.io/A40g2eh-SgG6uuvtulH4eg)
## 地面
* hierarchyで右クリックから、3Dオブジェクト、Planeを選択
* Scaleを自由に設定(推奨:x=10,y=1,z=10)
## player
### playerの配置
* [アセットストア](https://assetstore.unity.com/packages/3d/characters/free-mummy-monster-134212)から Add to My Assets をクリック
* Unity内でダウンロード&インポートを行う(window>package manager)
* Playerをhierarchyにドラッグアンドドロップ
### plyaerの制御
* Hierarchy > Mummy_Mon を選択
* Inspector > Add Component で Rigidbody 、Capsule Collider を追加
* Capsule Collider をキャラクターが中に入るように移動
* Project で右クリックし create > C# Script をクリック、「Move」と名前を付ける
* MoveスクリプトをMummy_monに取り付ける
### Move Script
```cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Rigidbody rd;
// 前進速度
public float forwardSpeed = 1.0f;
// 後退速度
public float backwardSpeed = 1.0f;
// 旋回速度
public float rotateSpeed = 1.0f;
// ジャンプ威力
public float jumpPower = 1.0f;
//キャラクターの移動
private Vector3 velocity;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
rd.constraints = RigidbodyConstraints.FreezeRotation; //転倒を防止
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
velocity = new Vector3(0, 0, v);
velocity = transform.TransformDirection(velocity);
if (v > 0.1)
{
velocity *= forwardSpeed; // 移動速度を掛ける
}
else if (v < -0.1)
{
velocity *= backwardSpeed; // 移動速度を掛ける
}
transform.localPosition += velocity * Time.fixedDeltaTime;
transform.Rotate(0, h * rotateSpeed, 0);
}
}
```
### アニメーション
* MUmmy_Mon > Animator > Contorollar に Mummy_Anim を付ける
## クリア
* ゴールとなる位置にCubeを配置する
* Congratulationsタグを作成し取り付ける
* is Triggerにチェック
* CongratulationsScriptを作成しMummy_Monに取り付ける
* Textを作成し、文字の位置が中央になるように調整する
* Congratulations Textに先ほどのTextを選択
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CongratulationsScript : MonoBehaviour
{
public Text CongratulationsText;
private void OnTriggerEnter(Collider other)
{
//クリア
if (other.gameObject.tag == "Congratulations")
{
//Destroy(this.gameObject);
CongratulationsText.text = "Congratulations";
}
}
}
```
## ゲームオーバー
* トラップとして任意の物体を配置する
* GameOverタグを作成し取り付ける
* is Triggerにチェック
* Texを作成し、文字の位置が中央に来るように調整する
* GameOverScriptを作成し、Mummy_Monに取り付ける
* GameOverTextに先ほど作ったTexを選択
## シーンの遷移
* シーンの名前をGameSceneに変更する
* TransitoinScriptを作成する
* hierarchyでCreate Emptyを選択
* Game Object に TransitoinScript を付ける
### タイトルシーン
* TitleSceneを作成
* Textを作成し、任意の文字を記入
* 新しく「TransitionScript2」を作成
* Create Empty をクリック
* Game Object に TransitoinScript2 を付ける
* File > Build Settings > Scenes In Build に GameScene , TitleScene を追加する
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TransitoinScript : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false; // UnityEditorの実行を停止
#else
Application.Quit(); // ゲームを終了
#endif
}
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene("GameScene");
}
}
}
```
### カメラの追尾設定
* カメラをplayerの子オブジェクトにする
* 方向や位置を調整する
### 接地判定
* Raycastによる接地判定のコードをMoveスクリプトに追記
* 接地判定時のみにジャンプできるように設定
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Rigidbody rd;
//接地判定
//public bool OnGround { get; set; }
// 前進速度
public float forwardSpeed = 2.0f;
// 後退速度
public float backwardSpeed = 1.0f;
// 旋回速度
public float rotateSpeed = 1.0f;
// ジャンプ威力
public float jumpPower = 5.0f;
//キャラクターの移動
private Vector3 velocity;
private float distance=1.0f;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
rd.constraints = RigidbodyConstraints.FreezeRotation; //転倒を防止
//OnGround = false;
}
// Update is called once per frame
void Update()
{
Vector3 rayPosition = transform.position + new Vector3(0.0f, 0.0f, 0.0f);
Ray ray = new Ray(rayPosition, Vector3.down);
bool isGround = Physics.Raycast(ray, distance);
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
velocity = new Vector3(0, 0, v);
velocity = transform.TransformDirection(velocity);
if (v > 0.1)
{
velocity *= forwardSpeed; // 移動速度を掛ける
}
else if (v < -0.1)
{
velocity *= backwardSpeed; // 移動速度を掛ける
}
transform.localPosition += velocity * Time.fixedDeltaTime;
transform.Rotate(0, h * rotateSpeed, 0);
if (Input.GetButtonDown("Jump") && isGround)
{
rd.velocity = transform.up * jumpPower;
}
Debug.Log(isGround);
}
}
```
## フィールド
* [Maze Generator(アセットストア)](https://assetstore.unity.com/packages/tools/modeling/maze-generator-38689)を使用し、ルートを作成する
* ゴールの位置などの調整を行う
---
https://hackmd.io/uToMjxxeRhKHxV1KYSV1JA