using UnityEngine; using UnityEngine.XR.Interaction.Toolkit; using UnityEngine.XR.Interaction.Toolkit.Interactables; public class CoinPickup : MonoBehaviour { [Header("Coin Setting")] public int coinValue = 1; [Header("Visual Spin")] public Transform visualRoot; public bool rotate = true; public float rotateSpeed = 180f; [Tooltip("°ÔÀÓ ÄÚÀÎó·³ ¼¼·Î·Î ºù±Ûºù±Û µ¹ ¶§ º¸Åë YÃà")] public Vector3 rotateAxis = Vector3.up; [Header("Float Motion")] public bool floatMotion = true; public float floatHeight = 0.08f; public float floatSpeed = 2f; [Header("Sound")] public AudioSource pickupSound; private bool isCollected = false; private Vector3 startPosition; private XRSimpleInteractable simpleInteractable; void Awake() { simpleInteractable = GetComponent(); if (simpleInteractable != null) { simpleInteractable.selectEntered.AddListener(OnSelected); } else { Debug.LogWarning("XRSimpleInteractable not found on coin."); } if (visualRoot == null) { visualRoot = transform; } } void OnDestroy() { if (simpleInteractable != null) { simpleInteractable.selectEntered.RemoveListener(OnSelected); } } void Start() { startPosition = transform.position; } void Update() { if (isCollected) { return; } if (rotate && visualRoot != null) { visualRoot.Rotate(rotateAxis.normalized, rotateSpeed * Time.deltaTime, Space.Self); } if (floatMotion) { float yOffset = Mathf.Sin(Time.time * floatSpeed) * floatHeight; transform.position = startPosition + new Vector3(0f, yOffset, 0f); } } private void OnSelected(SelectEnterEventArgs args) { Debug.Log("Coin selected by XR Simple Interactable."); CollectCoin(); } public void CollectCoin() { if (isCollected) { return; } isCollected = true; Debug.Log("CollectCoin called."); if (CoinManager.Instance != null) { CoinManager.Instance.AddCoin(coinValue); } else { Debug.LogWarning("CoinManager not found."); } HideCoinImmediately(); if (pickupSound != null && pickupSound.clip != null) { pickupSound.transform.parent = null; pickupSound.Play(); Destroy(pickupSound.gameObject, pickupSound.clip.length); } Destroy(gameObject); } void HideCoinImmediately() { MeshRenderer[] renderers = GetComponentsInChildren(); foreach (MeshRenderer renderer in renderers) { renderer.enabled = false; } Collider[] colliders = GetComponentsInChildren(); foreach (Collider collider in colliders) { collider.enabled = false; } Rigidbody rb = GetComponent(); if (rb != null) { rb.isKinematic = true; rb.useGravity = false; } if (simpleInteractable != null) { simpleInteractable.enabled = false; } } }