From ddef973b3ed888ec4e734bd825e6ef7b5e8a3dee Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Sat, 15 Feb 2025 00:42:19 +0800 Subject: [PATCH] game(scripts): socket -> RubbishBinSocket --- Game/Assets/Scripts/RubbishBinSocket.cs | 45 ++++++++++++++++++++ Game/Assets/Scripts/RubbishBinSocket.cs.meta | 11 +++++ 2 files changed, 56 insertions(+) create mode 100644 Game/Assets/Scripts/RubbishBinSocket.cs create mode 100644 Game/Assets/Scripts/RubbishBinSocket.cs.meta diff --git a/Game/Assets/Scripts/RubbishBinSocket.cs b/Game/Assets/Scripts/RubbishBinSocket.cs new file mode 100644 index 0000000..df6ccf2 --- /dev/null +++ b/Game/Assets/Scripts/RubbishBinSocket.cs @@ -0,0 +1,45 @@ +/* + * Author: Isaac + * Date: 7/2/25 + * Description: Script to handle the interaction between the rubbish bin and the trash objects + */ + +using UnityEngine; +using UnityEngine.XR.Interaction.Toolkit; +using UnityEngine.XR.Interaction.Toolkit.Interactables; +using UnityEngine.XR.Interaction.Toolkit.Interactors; + +public class RubbishBinSocket : MonoBehaviour +{ + private XRSocketInteractor _socket; + + private void Start() + { + _socket = GetComponent(); + + if (_socket == null) + { + Debug.LogError("XRSocketInteractor component is missing from the rubbish bin!"); + return; + } + + _socket.selectEntered.AddListener(OnTrashPlaced); + } + + private void OnDestroy() + { + if (_socket != null) _socket.selectEntered.RemoveListener(OnTrashPlaced); + } + + private void OnTrashPlaced(SelectEnterEventArgs args) + { + var trash = (XRBaseInteractable)args.interactableObject; + + if (trash == null) return; + Debug.Log($"{trash.gameObject.name} was placed in the rubbish bin and destroyed."); + Destroy(trash.gameObject); + + // Add haptic feedback if using a VR controller + if (args.interactorObject is XRBaseInputInteractor controller) controller.SendHapticImpulse(0.7f, 0.2f); + } +} \ No newline at end of file diff --git a/Game/Assets/Scripts/RubbishBinSocket.cs.meta b/Game/Assets/Scripts/RubbishBinSocket.cs.meta new file mode 100644 index 0000000..edc42cc --- /dev/null +++ b/Game/Assets/Scripts/RubbishBinSocket.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b3d5a817584b84da0a08814feb482587 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: