wirm/Game/Assets/Scripts/socket.cs

47 lines
1.4 KiB
C#
Raw Normal View History

2025-02-04 09:09:45 +08:00
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class RubbishBin : MonoBehaviour
2025-02-04 09:09:45 +08:00
{
private UnityEngine.XR.Interaction.Toolkit.Interactors.XRSocketInteractor socket;
2025-02-04 09:09:45 +08:00
private void Start()
{
socket = GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactors.XRSocketInteractor>();
if (socket == null)
{
Debug.LogError("XRSocketInteractor component is missing from the rubbish bin!");
return;
}
socket.selectEntered.AddListener(OnTrashPlaced);
2025-02-04 09:09:45 +08:00
}
private void OnTrashPlaced(SelectEnterEventArgs args)
2025-02-04 09:09:45 +08:00
{
UnityEngine.XR.Interaction.Toolkit.Interactables.XRBaseInteractable trash = (UnityEngine.XR.Interaction.Toolkit.Interactables.XRBaseInteractable)args.interactableObject;
if (trash != null)
2025-02-04 09:09:45 +08:00
{
Debug.Log($"{trash.gameObject.name} was placed in the rubbish bin and destroyed.");
Destroy(trash.gameObject);
2025-02-04 09:09:45 +08:00
// Add haptic feedback if using a VR controller
if (args.interactorObject is UnityEngine.XR.Interaction.Toolkit.Interactors.XRBaseInputInteractor controller)
2025-02-04 09:09:45 +08:00
{
controller.SendHapticImpulse(0.7f, 0.2f);
2025-02-04 09:09:45 +08:00
}
}
}
private void OnDestroy()
{
if (socket != null)
{
socket.selectEntered.RemoveListener(OnTrashPlaced);
}
2025-02-04 09:09:45 +08:00
}
}