So I have this script that is supposed to enable/disable a text game object stating “Press E To Interact.” Thanks to the Debug.Logs that I wrote, I noticed that “E disactive” (Yes English is my first language but I’m also sleep deprived why is why I didn’t write deactivated lol) always fires, but when in range of NPCS, “E active” fires followed immediately by “E disactive”
I’m unsure why, in range of NPCS, it’s still being registered that I’m in range then immediately out of range and going back and forth. If anyone can help, I’d appreciate it :]
public class PlayerInteract : MonoBehaviour {
public float npcRange = 2.75f;
public float itemRange = 1.75f;
public GameObject E2Interact;
//draw gizmos
void OnDrawGizmosSelected() {
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (transform.position, npcRange);
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, itemRange);
}
void Update () {
Collider[] npcArray = Physics.OverlapSphere (transform.position, npcRange);
Collider[] itemArray = Physics.OverlapSphere(transform.position, itemRange);
//INTERACTING WITH NPCS
foreach (Collider collider in npcArray) {
NPCInteractable npci = collider.GetComponent<NPCInteractable> ();
Dialogue dialogue = collider.GetComponent<Dialogue>();
//if (npci == null && dialogue == null) {
//E2Interact.SetActive(false);
//}
//now by pressing buttons
if (Input.GetButtonDown ("Interact")) {
if(npci != null) {
npci.Interact();
E2Interact.SetActive(false);
}
if (dialogue != null)
{
if (!dialogue.enabled)
{
E2Interact.SetActive(false);
dialogue.enabled = true;
dialogue.StartDialogue();
}
}
}
//stuff to happen without pressing any buttons
if (npci != null || dialogue != null)
{
E2Interact.SetActive(true);
Debug.Log("E active");
}
else
{
E2Interact.SetActive(false);
Debug.Log("E disactive");
}
if (npci != null)
{
npci.LookAt(this.transform);
}
}
foreach (Collider collider in itemArray)
{
ItemPickup item = collider.GetComponent<ItemPickup>();
if (item != null)
{
//SetFocus(interactable);
Debug.Log("iNTERACTABLE");
item.Interact();
}
}
}
}