using System.Collections; using UnityEngine; public class VRDoorController : MonoBehaviour { [Header("ドアオブジェクト")] [SerializeField] private Transform leftDoor; [SerializeField] private Transform rightDoor; [Header("動作設定")] [SerializeField] private float slideDistance = 1.0f; [SerializeField] private float openSpeed = 1.2f; [SerializeField] private float autoCloseDelay = 4.0f; private Vector3 leftDoorClosedPos; private Vector3 rightDoorClosedPos; private Vector3 leftDoorOpenPos; private Vector3 rightDoorOpenPos; private bool isOpen; private bool isMoving; private void Start() { if (leftDoor != null) { leftDoorClosedPos = leftDoor.localPosition; leftDoorOpenPos = leftDoorClosedPos + new Vector3(-slideDistance, 0, 0); } if (rightDoor != null) { rightDoorClosedPos = rightDoor.localPosition; rightDoorOpenPos = rightDoorClosedPos + new Vector3(slideDistance, 0, 0); } } public void OnButtonPress() { if (isMoving || isOpen) return; StartCoroutine(OpenAndAutoCloseRoutine()); } private IEnumerator OpenAndAutoCloseRoutine() { isMoving = true; float elapsedTime = 0f; while (elapsedTime < openSpeed) { elapsedTime += Time.deltaTime; float t = elapsedTime / openSpeed; float easedT = Mathf.SmoothStep(0f, 1f, t); if (leftDoor != null) leftDoor.localPosition = Vector3.Lerp(leftDoorClosedPos, leftDoorOpenPos, easedT); if (rightDoor != null) rightDoor.localPosition = Vector3.Lerp(rightDoorClosedPos, rightDoorOpenPos, easedT); yield return null; } if (leftDoor != null) leftDoor.localPosition = leftDoorOpenPos; if (rightDoor != null) rightDoor.localPosition = rightDoorOpenPos; isOpen = true; isMoving = false; yield return new WaitForSeconds(autoCloseDelay); isMoving = true; isOpen = false; elapsedTime = 0f; while (elapsedTime < openSpeed) { elapsedTime += Time.deltaTime; float t = elapsedTime / openSpeed; float easedT = Mathf.SmoothStep(0f, 1f, t); if (leftDoor != null) leftDoor.localPosition = Vector3.Lerp(leftDoorOpenPos, leftDoorClosedPos, easedT); if (rightDoor != null) rightDoor.localPosition = Vector3.Lerp(rightDoorOpenPos, rightDoorClosedPos, easedT); yield return null; } if (leftDoor != null) leftDoor.localPosition = leftDoorClosedPos; if (rightDoor != null) rightDoor.localPosition = rightDoorClosedPos; isMoving = false; } }