I have an object (let’s call it zone-object) that changes its boolean property to true if the player is within a certain distance and false when the player isn’t. This object is dotted around my environment. The idea with this is to let the player know what zone they’re in.
In this game object, I am doing this :
public float proximity; // Metres
public GameObject _player;
public bool user_detected = false;
public delegate void DetectionDelegate();
public event DetectionDelegate detectedEvent;
private void Update()
{
if (Vector3.Distance(_player.transform.position, transform.position) < proximity)
{
StartCoroutine(SendAlert());
Debug.DrawLine(transform.position, _player.transform.position, Color.green);
}
else if (Vector3.Distance(_player.transform.position, transform.position) > proximity)
{
StartCoroutine(StopAlert());
}
}
private IEnumerator StopAlert()
{
if (detectedEvent != null)
{
user_detected = false;
}
yield return null;
}
private IEnumerator SendAlert()
{
if (detectedEvent != null)
{
user_detected = true;
}
yield return null;
}
Now attached to my player object is a script that tries informs the player what zone they’re in. So if one of these zone object detects the player, it should set its boolean to true, and the player should be able to pick up this event and display the area they’re in (currently using the zone object’s name for now).
I just learned about using delegate/events although I am finding it hard to understand how I should write the code. Because I have multiple of these zone objects around the map, how would the player script know which zone object event is active? Would I need to List of some kind and iterate through it?
I’m open for suggestions, even if there is a simpler way to achieve what I’m doing. Bonus points if someone could explain delegates/events in simple english, although I get a general idea what they’re about, I need a simple example of a use case to fully grasp the idea.
Thanks!