using LiquidVolumeFX; using System; using System.Linq; using UnityEngine; using UnityEngine.XR.Interaction.Toolkit; using UnityEngine.XR.Interaction.Toolkit.Interactables; public enum TrashBinType { SGR, Sticla, HartieCarton, PlasticMetal, Rezidual, Biodegradabil } public class Trash : MonoBehaviour { [Header("Trash Settings")] public bool hasBeenCollected = false; public TrashBinType trashType; [Header("Liquid Volume Settings(DO NOT ASSIGN IN INSPECTOR!)")] public LiquidVolume liquidVolume; private Vector3 portalPosition = new Vector3(-0.75626421f, 1.5f, 4.1960001f); private Rigidbody rb; private XRGrabInteractable grabInteractable; private void Start() { if(GetComponent() != null) { rb = GetComponent(); } else { Debug.LogError("Rigidbody component is missing on the Trash object. Please add a Rigidbody component to the Trash object."); } if(GetComponent() != null) { grabInteractable = GetComponent(); } else { Debug.LogError("XRGrabInteractable component is missing on the Trash object. Please add a XRGrabInteractable component to the Trash object."); } grabInteractable.selectEntered.AddListener(OnSelectEnter); grabInteractable.selectExited.AddListener(OnSelectExit); //Exclude the layer named "StopperLayer" GameObject root = transform.root.gameObject; //Get the collider of the root object and of its children Collider[] colliders = root.GetComponentsInChildren(); colliders.Append(root.GetComponent()); foreach (Collider collider in colliders) { collider.excludeLayers = LayerMask.GetMask("StopperLayer"); } //Get the rigidbody of the root object and of its children Rigidbody[] rigidbodies = root.GetComponentsInChildren(); rigidbodies.Append(root.GetComponent()); foreach (Rigidbody rb in rigidbodies) { rb.excludeLayers = LayerMask.GetMask("StopperLayer"); } if(GetComponentInChildren() != null) { liquidVolume = GetComponentInChildren(); } if(GetComponent() != null) { liquidVolume = GetComponentInChildren(); } } private void Update() { // If the trash is too far away, move at Vector3(-0.75626421,1.5,4.1960001) if(Vector3.Distance(transform.position, portalPosition) > 17f) { rb.velocity = Vector3.zero; transform.position = portalPosition; } } private void OnSelectEnter(SelectEnterEventArgs args) { Debug.Log("Holding trash!"); // For all rigidbodies on this transform and children, ignore collisions with "Ignore Raycast" layer Collider[] colliders = GetComponentsInChildren(); foreach (Collider collider in colliders) { collider.excludeLayers = LayerMask.GetMask("Ignore Raycast"); collider.excludeLayers += LayerMask.GetMask("StopperLayer"); // Add StopperLayer to the exclude layers } // For all rigidbodies on this transform and children, ignore collisions with "StopperLayer" layer Rigidbody[] rigidbodies = GetComponentsInChildren(); foreach (Rigidbody rb in rigidbodies) { rb.excludeLayers = LayerMask.GetMask("Ignore Raycast"); rb.excludeLayers += LayerMask.GetMask("StopperLayer"); // Add StopperLayer to the exclude layers } Collider[] parentColliders = GetComponents(); foreach (Collider collider in parentColliders) { collider.excludeLayers = LayerMask.GetMask("Ignore Raycast"); collider.excludeLayers += LayerMask.GetMask("StopperLayer"); // Add StopperLayer to the exclude layers } Rigidbody[] parentRigidbodies = GetComponents(); foreach (Rigidbody rb in parentRigidbodies) { rb.excludeLayers = LayerMask.GetMask("Ignore Raycast"); rb.excludeLayers += LayerMask.GetMask("StopperLayer"); // Add StopperLayer to the exclude layers } } private void OnSelectExit(SelectExitEventArgs args) { Debug.Log("Not holding trash anymore!"); // Enable collision Collider[] colliders = GetComponentsInChildren(); foreach (Collider collider in colliders) { collider.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask } Rigidbody[] rigidbodies = GetComponentsInChildren(); foreach (Rigidbody rb in rigidbodies) { rb.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask } Collider[] parentColliders = GetComponents(); foreach (Collider collider in parentColliders) { collider.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask } Rigidbody[] parentRigidbodies = GetComponents(); foreach (Rigidbody rb in parentRigidbodies) { rb.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask } } }