using System; using System.Collections.Generic; using UnityEngine; public class PlayerTalkRange : MonoBehaviour { private List npcsInRange = new(); //also indicate the npc you're closest to [Header("UI")] [SerializeField] private Transform talkPopupUI; [SerializeField] private Vector3 uiOffset; private void OnTriggerEnter2D(Collider2D other) { if (other.TryGetComponent(out NPC isNPC) && !npcsInRange.Contains(isNPC)) //placeholder key lol { npcsInRange.Add(isNPC); } } private void OnTriggerExit2D(Collider2D other) { if (other.TryGetComponent(out NPC isNPC) && npcsInRange.Contains(isNPC)) //placeholder key lol { npcsInRange.Remove(isNPC); if (npcsInRange.Count == 0) { talkPopupUI.gameObject.SetActive(false); } } } private void Update() { if (npcsInRange.Count > 0) { NPC closestNPC = null; foreach (NPC npc in npcsInRange) { if (!closestNPC || Vector3.Distance(closestNPC.transform.position, transform.position) > Vector3.Distance(npc.transform.position, transform.position)) { closestNPC = npc; } } if (closestNPC) { if (!talkPopupUI.gameObject.activeSelf) { talkPopupUI.gameObject.SetActive(true); } talkPopupUI.position = closestNPC.transform.position + uiOffset; } if (DialogueManager.instance.currentDialogueScript == null && Input.GetKeyDown(KeyCode.E)) { closestNPC.StartDialogue(); } } } }