Hello everyone, I’ve been working on a Systematic Dialogue that Plays out without the player pressing any buttons (meaning it automatically plays when he collides in another NPC) but what I find Happening is that the Player can quite easily spam the NPC dialogue which I don’t want.
So I introduced a new Coroutine called WaitBeforeReplay()
but and place the Start Coroutine in theOnTriggerExit
function, but it doesn’t work. How can I go about this?:
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GreetKojo : MonoBehaviour
{
private Animator anim;
public GameObject NPC;// So we can use this script on multiple NPC’s instead of doing multiple scripts
public GameObject Dialogue;
public GameObject KojoDialogue;// Dialog for Kojo
public float waitTime;`
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
}
//When collision is made, trigger the greeting animation and show pop up text
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
anim.SetBool("Notice", true);//Start animation loop in bool
Dialogue.SetActive(true);//Hide pop up text and disxontinue greeting animation
}
}
//Whenever the Player, exits the trigger resume regular idle loop
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
anim.SetBool("Notice", false);//Stop animation looping bool
Dialogue.SetActive(false);// Deactivates the canvas after Player leaves collision
StartCoroutine(KojoRes());
}
}
//Deactivates KoJo's Dialogue
private IEnumerator KojoOff()
{
yield return new WaitForSeconds(waitTime);
StartCoroutine(WaitBeforeReplay());
KojoDialogue.SetActive(false);
}
//Activates KoJo's Dialogue
IEnumerator KojoRes()
{
yield return new WaitForSeconds(2);//Wait for x amount of seconds
KojoDialogue.SetActive(true);//Then set Kojo's dialogue on
StartCoroutine(KojoOff());//After which we wait x amount to deactivate the dialogue
}
private IEnumerator WaitBeforeReplay()
{
//Wait for x amount of time before allowing dialogue to appear again
yield return new WaitForSeconds(6);
GetComponent<Collider2D>().enabled = false;
}
}
</code></code></code></code>