ボスステージが壊れていた
リカバリーしてエレベーターを作った
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.XR; // 振動機能のために必要
|
||||
using UnityEngine.XR;
|
||||
using System.Collections;
|
||||
|
||||
public class ElevatorSceneTransition : MonoBehaviour
|
||||
@@ -15,32 +15,33 @@ public class ElevatorSceneTransition : MonoBehaviour
|
||||
[SerializeField] private Text progressText;
|
||||
[SerializeField] private Slider progressSlider;
|
||||
|
||||
// ─── ★ここから下降演出用の設定項目を追加 ───
|
||||
[Header("エレベーター下降演出設定")]
|
||||
[Tooltip("エレベーターの部屋(壁や床、UIすべて)をまとめている一番親のTransformを指定してください")]
|
||||
[Tooltip("エレベーターの部屋(壁や床、UIすべて)をまとめているオブジェクトを指定してください(※プレイヤーは外しておきます)")]
|
||||
[SerializeField] private Transform elevatorCageTransform;
|
||||
|
||||
[Tooltip("ガタガタ揺れる激しさ(ミリ単位。0.01~0.03あたりがVR酔いしにくく自然です)")]
|
||||
[Tooltip("ガタガタ揺れる激しさ(0.01~0.03あたりが自然です)")]
|
||||
[SerializeField] private float shakeMagnitude = 0.015f;
|
||||
|
||||
[Tooltip("下降中のコントローラーの振動の強さ(0.0 ~ 1.0)")]
|
||||
[SerializeField] private float hapticAmplitude = 0.15f;
|
||||
// ─────────────────────────────────────────
|
||||
|
||||
// ─── ★新設:親子関係にしないためのプレイヤー登録枠 ───
|
||||
[Header("プレイヤーの同期設定")]
|
||||
[Tooltip("XR Origin(またはXR Rig)のTransformを直接ドラッグ&ドロップしてください")]
|
||||
[SerializeField] private Transform playerTransform;
|
||||
// ─────────────────────────────────────────────────────
|
||||
|
||||
private bool isLoadingStarted = false;
|
||||
private Vector3 originalCagePosition; // 揺らす前のエレベーターの初期位置を記憶する用
|
||||
private Vector3 originalCagePosition;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (loadingCanvas != null)
|
||||
{
|
||||
loadingCanvas.SetActive(false);
|
||||
}
|
||||
if (loadingCanvas != null) loadingCanvas.SetActive(false);
|
||||
|
||||
// 部屋の初期位置を保存
|
||||
if (elevatorCageTransform != null)
|
||||
{
|
||||
originalCagePosition = elevatorCageTransform.localPosition;
|
||||
// ワールド空間での初期位置を記憶(localPositionからpositionに変更し、より確実に)
|
||||
originalCagePosition = elevatorCageTransform.position;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +52,14 @@ public class ElevatorSceneTransition : MonoBehaviour
|
||||
if (other.CompareTag(playerTag))
|
||||
{
|
||||
isLoadingStarted = true;
|
||||
Debug.Log("🛗 プレイヤー乗車:下降演出と非同期ロードを開始します。");
|
||||
|
||||
// ★安全対策:接触したプレイヤーのTransformを自動でキャッシュする
|
||||
if (playerTransform == null)
|
||||
{
|
||||
playerTransform = other.transform;
|
||||
}
|
||||
|
||||
Debug.Log("🛗 プレイヤー乗車:親子関係なしのデルタ同期で下降演出を開始します。");
|
||||
StartCoroutine(LoadSceneAsyncRoutine());
|
||||
}
|
||||
}
|
||||
@@ -60,34 +68,42 @@ public class ElevatorSceneTransition : MonoBehaviour
|
||||
{
|
||||
if (loadingCanvas != null) loadingCanvas.SetActive(true);
|
||||
|
||||
// 次のシーンの非同期ロードを開始(画面切り替えはまだロックする)
|
||||
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(bossSceneName);
|
||||
asyncLoad.allowSceneActivation = false;
|
||||
|
||||
// コントローラーの振動のタイミングを計るためのタイマー
|
||||
float hapticTimer = 0f;
|
||||
|
||||
// ロード中(progressが0.9未満の間)、ずっとループして演出を実行
|
||||
while (asyncLoad.progress < 0.9f)
|
||||
{
|
||||
// 1. UIの進捗率(%)を更新
|
||||
float currentProgress = Mathf.Clamp01(asyncLoad.progress / 0.9f);
|
||||
if (progressText != null) progressText.text = $"LOADING: {(currentProgress * 100f):F0}%";
|
||||
if (progressSlider != null) progressSlider.value = currentProgress;
|
||||
|
||||
// 2. 【演出:部屋をガタガタ揺らす】
|
||||
// 初期位置をベースに、毎フレームごくわずかなランダム座標を計算して部屋を揺らす
|
||||
// ─── ★【超重要】親子関係なしでプレイヤーを完全同期させるロジック ───
|
||||
|
||||
// 1. 動かす前の、このフレームにおけるエレベーターの現在位置を記録
|
||||
Vector3 previousCagePosition = elevatorCageTransform.position;
|
||||
|
||||
// 2. エレベーターの部屋だけをランダムにガタガタ動かす
|
||||
if (elevatorCageTransform != null)
|
||||
{
|
||||
Vector3 randomOffset = Random.insideUnitSphere * shakeMagnitude;
|
||||
// ※左右前後のブレ(X, Z)のみを揺らし、Y(上下)はあえて固定すると「ガタガタ感」が出ます
|
||||
randomOffset.y = 0;
|
||||
elevatorCageTransform.localPosition = originalCagePosition + randomOffset;
|
||||
randomOffset.y = 0; // 横揺れのみ
|
||||
elevatorCageTransform.position = originalCagePosition + randomOffset;
|
||||
}
|
||||
|
||||
// 3. 【演出:両手のコントローラーを定期的に微振動させる】
|
||||
// 3. 「この1フレームでエレベーターが実際に動いた移動差分(デルタ)」を計算
|
||||
Vector3 cageDelta = elevatorCageTransform.position - previousCagePosition;
|
||||
|
||||
// 4. その移動差分を、プレイヤーの座標にもそのまま上乗せして足し算する
|
||||
if (playerTransform != null && cageDelta != Vector3.zero)
|
||||
{
|
||||
playerTransform.position += cageDelta;
|
||||
}
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
|
||||
hapticTimer += Time.deltaTime;
|
||||
if (hapticTimer >= 0.1f) // 0.1秒ごとにブルブルと脈打つような振動を送る
|
||||
if (hapticTimer >= 0.1f)
|
||||
{
|
||||
TriggerBothHandHaptics(hapticAmplitude, 0.08f);
|
||||
hapticTimer = 0f;
|
||||
@@ -96,34 +112,30 @@ public class ElevatorSceneTransition : MonoBehaviour
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// ─── ロード完了後の処理 ───
|
||||
|
||||
// 4. 【演出の停止】エレベーターの揺れをピタッと止め、位置を綺麗にリセットする
|
||||
// 到着後、エレベーターの位置を元の位置に綺麗に戻す
|
||||
if (elevatorCageTransform != null)
|
||||
{
|
||||
elevatorCageTransform.localPosition = originalCagePosition;
|
||||
// 最後の移動差分を計算してプレイヤーを戻す
|
||||
Vector3 previousCagePosition = elevatorCageTransform.position;
|
||||
elevatorCageTransform.position = originalCagePosition;
|
||||
Vector3 cageDelta = elevatorCageTransform.position - previousCagePosition;
|
||||
if (playerTransform != null) playerTransform.position += cageDelta;
|
||||
}
|
||||
|
||||
if (progressText != null) progressText.text = "ARRIVED";
|
||||
if (progressSlider != null) progressSlider.value = 1f;
|
||||
|
||||
// 到着後の余韻(ガタガタが止まってから、扉が開いてシーンが変わるまでの静寂1秒)
|
||||
yield return new WaitForSeconds(1.0f);
|
||||
|
||||
Debug.Log("🎉 ボスステージに到着。シーンを切り替えます。");
|
||||
asyncLoad.allowSceneActivation = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 左右両方のコントローラーを同時に微振動させるヘルパーメソッド
|
||||
/// </summary>
|
||||
private void TriggerBothHandHaptics(float amplitude, float duration)
|
||||
{
|
||||
// 左手
|
||||
InputDevice leftDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
|
||||
if (leftDevice.isValid) leftDevice.SendHapticImpulse(0, amplitude, duration);
|
||||
|
||||
// 右手
|
||||
InputDevice rightDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
|
||||
if (rightDevice.isValid) rightDevice.SendHapticImpulse(0, amplitude, duration);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user