145 lines
3.3 KiB
C#
145 lines
3.3 KiB
C#
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<XRSimpleInteractable>();
|
|
|
|
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<MeshRenderer>();
|
|
foreach (MeshRenderer renderer in renderers)
|
|
{
|
|
renderer.enabled = false;
|
|
}
|
|
|
|
Collider[] colliders = GetComponentsInChildren<Collider>();
|
|
foreach (Collider collider in colliders)
|
|
{
|
|
collider.enabled = false;
|
|
}
|
|
|
|
Rigidbody rb = GetComponent<Rigidbody>();
|
|
if (rb != null)
|
|
{
|
|
rb.isKinematic = true;
|
|
rb.useGravity = false;
|
|
}
|
|
|
|
if (simpleInteractable != null)
|
|
{
|
|
simpleInteractable.enabled = false;
|
|
}
|
|
}
|
|
} |