using UnityEngine; using System.Collections; [RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(Collider))] public class BoxController : MonoBehaviour { [Tooltip("박스가 떨어진 후 사라졌다가 다시 생성될 때까지 걸리는 시간(초)")] [SerializeField] private float resetDelay = 3.0f; [Tooltip("로봇 팔에 사용될 태그")] [SerializeField] private string robotArmTag = "RobotArm"; [Tooltip("박스를 내려놓을 공간에 사용될 태그")] [SerializeField] private string dropZoneTag = "DropZone"; private Vector3 originalPosition; private Quaternion originalRotation; private Transform attachParent = null; private Rigidbody rb; private bool isResetting = false; void Start() { originalPosition = transform.position; originalRotation = transform.rotation; rb = GetComponent(); Collider col = GetComponent(); if (!col.isTrigger) { Debug.LogWarning($"'{gameObject.name}'의 Collider가 'Is Trigger'로 설정되어 있지 않습니다. 자동 설정합니다.", this); col.isTrigger = true; } } private void OnTriggerEnter(Collider other) { if (isResetting) return; // 로봇 팔에 붙기 if (other.CompareTag(robotArmTag) && attachParent == null) { Debug.Log("로봇 팔 감지. 박스를 부착합니다."); // 로봇 팔을 부모로 설정하여 따라다니게 함 attachParent = other.transform; transform.SetParent(attachParent); transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity); // 부착된 동안 물리적 충돌이나 중력을 무시하도록 Rigidbody를 Kinematic으로 설정 if (rb != null) rb.isKinematic = true; } // 드랍 존에서 떨어지기 if (other.CompareTag(dropZoneTag) && attachParent != null) { Debug.Log("드랍 존 감지. 박스를 분리하고 리셋 타이머를 시작합니다."); // 부모(로봇 팔)와의 연결을 끊음 transform.SetParent(null); attachParent = null; // Rigidbody의 Kinematic을 해제하여 중력 등의 물리 효과를 다시 받음 if (rb != null) rb.isKinematic = false; // 리셋 코루틴 시작 isResetting = true; StartCoroutine(ResetAfterDelay()); } } private IEnumerator ResetAfterDelay() { yield return new WaitForSeconds(resetDelay); gameObject.SetActive(false); // 기존 위치에 다시 생김 // 위치/회전 리셋 transform.position = originalPosition; transform.rotation = originalRotation; // Rigidbody 상태 리셋 (떨어지던 속도 제거) if (rb != null) { rb.isKinematic = false; rb.linearVelocity = Vector3.zero; rb.angularVelocity = Vector3.zero; } gameObject.SetActive(true); isResetting = false; Debug.Log("박스가 원위치로 리셋되었습니다."); } }