I’m trying to call events from various circumstances.
I have a custom EventManager set up from a tutorial. It’s working fine; I call the first event when the player enters a Trigger, which causes an NPC to approach him and say something. The problem is I want to call the second event when the player reaches a certain distance from the NPC, but I’m not sure how to go about it. I’ve tried a coroutine but I’m very new to them so I’m not sure if it’s the best way to go about this. I’m also not sure how I would go about adding a function callback so I can trigger the desired event from the coroutine. Can anyone help me?
I’m not sure which code would be helpful, I’ve added my Events code below, if there’s anything else you’d like to see just let me know and I’ll add it here.
public class Events : MonoBehaviour {
private UnityAction event01;
private UnityAction event02;
public Transform player;
public CompanionAI Laurelai;
void Awake()
{
event01 = new UnityAction(Event01Function);
event02 = new UnityAction(Event02Function);
}
void OnEnable()
{
EventManager.StartListening("Event01", event01);
}
void OnDisable()
{
EventManager.StopListening("Event01", event01);
}
IEnumerator WaitForDistance(Vector3 a, Vector3 b, float distanceToCheck) //function callBack?
{
while(Vector3.Distance(a, b) < distanceToCheck)
{
yield return null;
}
Debug.Log("Distance reached");
//I thought of adding some kind of function callback here so I could call a function to trigger the next event, but I don't know how to go about it.
}
void Event01Function()
{
//Debug.Log("Event01 called");
Laurelai.followTarget = player;
Laurelai.LookAtPlayer(true);
Laurelai.isFollowing = true;
Laurelai.Talk();
EventManager.StartListening("Event02", event02);
StartCoroutine(WaitForDistance(Laurelai.transform.position, player.position, Laurelai.followDistance));
//EventManager.TriggerEvent("Event02");
}
void Event02Function()
{
Laurelai.Talk();
}
}
Maybe I’m going about the whole thing the wrong way? Thanks for any help anyone can give me!