UnityGameProjectsCode / NightwatchGame / TrashBin.cs
TrashBin.cs
Raw
using UnityEngine;

public class TrashBin : MonoBehaviour
{
    //allow the player to pick the bin up
    private bool pickedUp;
    private bool inDumpsterArea;
    private bool inTrashBinPickupArea;
    private bool binTaken;
    public GameObject playerBin;
    public GameObject dumpsterBin;
    public Dumpster dumpster;
    private bool trashBinEnabled;
    public GameObject interactionUI;
    public InteractionTextControl textControl;
    public AudioSource pickupSound;

    public bool TrashBinEnabled { get => trashBinEnabled; set => trashBinEnabled = value; }
    public bool InDumpsterArea { get => inDumpsterArea; set => inDumpsterArea = value; }
    public bool PickedUp { get => pickedUp; set => pickedUp = value; }

    private void OnTriggerEnter(Collider other)
    {
        if (!binTaken)
        {
            inTrashBinPickupArea = true;
            interactionUI.SetActive(true);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (!binTaken)
        {
            inTrashBinPickupArea = false;
            interactionUI.SetActive(false);
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && !trashBinEnabled && inTrashBinPickupArea)
        {
            textControl.DisplayText("Filled with trash.");
        }

        if (Input.GetKeyDown(KeyCode.E) && trashBinEnabled && pickedUp == false && inTrashBinPickupArea)
        {
            binTaken = true;
            inTrashBinPickupArea = false;
            pickedUp = true;
            playerBin.SetActive(true);
            pickupSound.Play();
            gameObject.GetComponent<BoxCollider>().enabled = false;

            foreach (MeshRenderer childMesh in GetComponentsInChildren<MeshRenderer>())
            {
                childMesh.enabled = false;
            }

            interactionUI.SetActive(false);
        }

        if (pickedUp && Input.GetKeyDown(KeyCode.E) && inDumpsterArea)
        {
            dumpsterBin.SetActive(true);
            playerBin.SetActive(false);
            pickedUp = false;
            dumpster.PlayTrashSound();
            interactionUI.SetActive(false);
        }
    }

    public void SetInDumpsterArea(bool state)
    {
        inDumpsterArea = state;
    }
}