using UnityEngine; public class SimpleAutoCloseDoor : MonoBehaviour { [Header("Auto-Close Settings")] public float autoCloseDelay = 3f; public float detectionRadius = 4f; private SimpleDoorInteraction doorScript; private GameObject player; private float autoCloseTimer = 0f; void Start() { // Get the door script doorScript = GetComponent(); if (doorScript == null) { Debug.LogError("SimpleAutoCloseDoor requires SimpleDoorInteraction component!"); enabled = false; return; } // Find player player = GameObject.FindGameObjectWithTag("Player"); if (player == null) { Debug.LogWarning("No GameObject with 'Player' tag found for auto-close door detection."); } } void Update() { if (doorScript == null || player == null) return; // Only handle auto-close if door is open if (doorScript.isOpen && !doorScript.isAnimating) { float distance = Vector3.Distance(transform.position, player.transform.position); // If player is far enough away, start timer if (distance > detectionRadius) { autoCloseTimer += Time.deltaTime; if (autoCloseTimer >= autoCloseDelay) { doorScript.ToggleDoor(); autoCloseTimer = 0f; } } else { // Reset timer if player comes back autoCloseTimer = 0f; } } else { autoCloseTimer = 0f; } } void OnDrawGizmosSelected() { // Draw auto-close detection radius Gizmos.color = Color.cyan; Gizmos.DrawWireSphere(transform.position, detectionRadius); } }