ici_recycle_vr/Assets/Scripts/Trash.cs
Diaconescu Andrei-Alexandru 19674bfe5d Am adaugat ICI Recycle
2025-05-26 12:54:36 +03:00

159 lines
5.4 KiB
C#

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<Rigidbody>() != null)
{
rb = GetComponent<Rigidbody>();
}
else
{
Debug.LogError("Rigidbody component is missing on the Trash object. Please add a Rigidbody component to the Trash object.");
}
if(GetComponent<XRGrabInteractable>() != null)
{
grabInteractable = GetComponent<XRGrabInteractable>();
}
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<Collider>();
colliders.Append(root.GetComponent<Collider>());
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<Rigidbody>();
rigidbodies.Append(root.GetComponent<Rigidbody>());
foreach (Rigidbody rb in rigidbodies)
{
rb.excludeLayers = LayerMask.GetMask("StopperLayer");
}
if(GetComponentInChildren<LiquidVolume>() != null)
{
liquidVolume = GetComponentInChildren<LiquidVolume>();
}
if(GetComponent<LiquidVolume>() != null)
{
liquidVolume = GetComponentInChildren<LiquidVolume>();
}
}
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<Collider>();
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<Rigidbody>();
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<Collider>();
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<Rigidbody>();
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<Collider>();
foreach (Collider collider in colliders)
{
collider.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask
}
Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
foreach (Rigidbody rb in rigidbodies)
{
rb.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask
}
Collider[] parentColliders = GetComponents<Collider>();
foreach (Collider collider in parentColliders)
{
collider.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask
}
Rigidbody[] parentRigidbodies = GetComponents<Rigidbody>();
foreach (Rigidbody rb in parentRigidbodies)
{
rb.excludeLayers = LayerMask.GetMask("StopperLayer"); // Reset to default layer mask
}
}
}