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

public class Dumpster : MonoBehaviour
{
    public TrashBin trashBin;
    private AudioSource dumpsterSound;
    public GameObject interactionUI;
    public InteractionTextControl textControl;
    private bool playerNearby;

    private void Awake()
    {
        dumpsterSound = GetComponent<AudioSource>();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && trashBin.PickedUp && playerNearby)
        {
            GetComponent<BoxCollider>().enabled = false;
            interactionUI.SetActive(false);
            playerNearby = false;
        }

        if (Input.GetKeyDown(KeyCode.E) && !trashBin.PickedUp && playerNearby)
        {
            textControl.DisplayText("Rusty old dumpster.");
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        trashBin.SetInDumpsterArea(true);
        interactionUI.SetActive(true);
        playerNearby = true;
    }

    private void OnTriggerExit(Collider other)
    {
        trashBin.SetInDumpsterArea(false);
        interactionUI.SetActive(false);
        playerNearby = false;
    }

    public void PlayTrashSound()
    {
        dumpsterSound.Play();
    }
}