Unityで立体迷路を作る⑧|ゴール判定とクリア後の演出を作成する

Unity立体迷路 ゴール後のカメラ演出画面 ゲームの作り方
ゴール後のカメラ演出画面

🔍本記事では、RigidbodyやCharacterControllerを使わずにC#スクリプトだけで立体迷路を作る方法を解説しています。
 
今回は「ゴール判定」と「クリア後の演出」を実装していきます。

✅前回の記事はこちら👇

スポンサーリンク

スタートとゴールのオブジェクトを作成

スタート位置

  • 3Dオブジェクト → キューブや平面を追加
  • 入り口部分を壁で囲み、Wallレイヤー+メッシュコライダー を設定
     → ゴール付近回り込みできないようにする
  • 3Dオブジェクト→テキストを追加
  • テキストを「Start」にして位置を調整
Unity立体迷路 スタート位置となるオブジェクトを作成した画像

ゴール位置

  • キューブなどで出口部分を作成
  • Boxコライダー(トリガー付き を追加し、プレイヤーの侵入を判定
Unity立体迷路 ゴール位置となるオブジェクトを作成した画像

ゴール後のUIパネルを作成

  • UI → パネルを追加
  • 「Game Clear」テキストを表示
  • クリアタイムを表示するTextMeshPro を設置
Unity立体迷路 ゴール後のUIパネルを作成した画像

ゴール後のカメラ演出

立体迷路を回転しながら写す演出を作成します。

セットアップ

  • 空のオブジェクトを作成し「cameraPivot」と命名
  • 子オブジェクトに イベント用カメラ を追加
  • 迷路全体が見える位置に配置
Unity立体迷路 ゴール後のカメラ演出用に迷路全体が見える位置にカメラをセットした画像

CameraEventController.cs

using UnityEngine;

public class CameraEventController : MonoBehaviour
{
public Camera playerCamera;
public Camera eventCamera;
public Transform cameraPivot; // 演出用カメラの親オブジェクト
public float rotationSpeed = 20f; // 回転速度

private bool isRotating = false;

void Update()
{
if (isRotating)
{
cameraPivot.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
}
}

public void StartGame()
{
playerCamera.enabled = true;
eventCamera.enabled = false;
}

public void StartCameraEvent()
{
playerCamera.enabled = false;
eventCamera.enabled = true;
isRotating = true;
}

ゴール判定用のスクリプト

GoalTrigger.cs

using UnityEngine;
using System.Collections;
using TMPro;

public class GoalTrigger : MonoBehaviour
{
public LayerMask playerLayer;
public GameObject mainPanel;
public GameObject goalPanel;
public TextMeshProUGUI goalTimeText;
public PlayerMover playerMover;
public float goalRadius = 3f; // ゴール範囲を広く設定
public static bool hasCleared = false;

private void Start()
{
 FindObjectOfType<CameraEventController>().StartGame();
}

void Update()
{
 if (hasCleared) return;

Collider[] hits = Physics.OverlapBox(transform.position,
new Vector3(goalRadius, goalRadius, goalRadius), Quaternion.identity, playerLayer);

 if (hits.Length > 0)
  {
  hasCleared = true;
  StartCoroutine(WaitAndGoalEvent(1f));
  }
}

IEnumerator WaitAndGoalEvent(float waitTime)
{
yield return new WaitForSeconds(waitTime); // 指定した時間待機
FindObjectOfType<CameraEventController>().StartCameraEvent();

goalPanel.SetActive(true);
mainPanel.SetActive(false);

float clearTime = playerMover.GetPlayTime();
int minutes = Mathf.FloorToInt(clearTime / 60f);
int seconds = Mathf.FloorToInt(clearTime % 60f);
goalTimeText.text = $”Clear Time: {minutes:00}:{seconds:00}”;
}

PlayerMoverスクリプトに追記

public float GetPlayTime()
{
return playerTime;
}

動作確認ポイント

  • プレイヤーがゴールに触れて1秒後、ゴール用パネルが表示されるか
  • ゴール時にカメラが切り替わり、迷路全体を回転しながら写す
  • クリアタイムが正しく表示されるか

まとめ

今回は、

  • ゴール判定の実装
  • クリア後のUI表示
  • カメラによる演出

を実装しました。

次回は「トゥルーエンド演出」として、インプットフィールドを使った仕掛け を解説していきます。

コメント

タイトルとURLをコピーしました