119 lines
3.9 KiB
C#
119 lines
3.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.XR;
|
|
|
|
public class ElevatorSceneTransition : MonoBehaviour
|
|
{
|
|
[Header("シーン設定")]
|
|
[SerializeField] private string bossSceneName = "BossStage";
|
|
[SerializeField] private string playerTag = "Player";
|
|
|
|
[Header("UI設定")]
|
|
[SerializeField] private GameObject loadingCanvas;
|
|
[SerializeField] private Text progressText;
|
|
[SerializeField] private Slider progressSlider;
|
|
|
|
[Header("演出設定")]
|
|
[SerializeField] private Transform elevatorCageTransform;
|
|
[SerializeField] private float shakeMagnitude = 0.015f;
|
|
[SerializeField] private float hapticAmplitude = 0.15f;
|
|
[SerializeField] private Transform playerTransform;
|
|
|
|
private bool isLoadingStarted;
|
|
private Vector3 originalCagePosition;
|
|
|
|
private void Start()
|
|
{
|
|
if (loadingCanvas != null) loadingCanvas.SetActive(false);
|
|
|
|
if (elevatorCageTransform != null)
|
|
{
|
|
originalCagePosition = elevatorCageTransform.position;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (isLoadingStarted) return;
|
|
|
|
if (other.CompareTag(playerTag))
|
|
{
|
|
isLoadingStarted = true;
|
|
|
|
if (playerTransform == null)
|
|
{
|
|
playerTransform = other.transform;
|
|
}
|
|
|
|
StartCoroutine(LoadSceneAsyncRoutine());
|
|
}
|
|
}
|
|
|
|
private IEnumerator LoadSceneAsyncRoutine()
|
|
{
|
|
if (loadingCanvas != null) loadingCanvas.SetActive(true);
|
|
|
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(bossSceneName);
|
|
asyncLoad.allowSceneActivation = false;
|
|
|
|
float hapticTimer = 0f;
|
|
|
|
while (asyncLoad.progress < 0.9f)
|
|
{
|
|
float currentProgress = Mathf.Clamp01(asyncLoad.progress / 0.9f);
|
|
if (progressText != null) progressText.text = $"LOADING: {(currentProgress * 100f):F0}%";
|
|
if (progressSlider != null) progressSlider.value = currentProgress;
|
|
|
|
Vector3 previousCagePosition = elevatorCageTransform.position;
|
|
|
|
if (elevatorCageTransform != null)
|
|
{
|
|
Vector3 randomOffset = Random.insideUnitSphere * shakeMagnitude;
|
|
randomOffset.y = 0;
|
|
elevatorCageTransform.position = originalCagePosition + randomOffset;
|
|
}
|
|
|
|
Vector3 cageDelta = elevatorCageTransform.position - previousCagePosition;
|
|
|
|
if (playerTransform != null && cageDelta != Vector3.zero)
|
|
{
|
|
playerTransform.position += cageDelta;
|
|
}
|
|
|
|
hapticTimer += Time.deltaTime;
|
|
if (hapticTimer >= 0.1f)
|
|
{
|
|
TriggerBothHandHaptics(hapticAmplitude, 0.08f);
|
|
hapticTimer = 0f;
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
|
|
if (elevatorCageTransform != null)
|
|
{
|
|
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;
|
|
|
|
yield return new WaitForSeconds(1.0f);
|
|
|
|
asyncLoad.allowSceneActivation = true;
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |