I’m trying to use a Spherecast to trigger “OnDetected” and “OnUndetected” events for when a “CurrentTarget” variable gets changed,
So far I have the OnDetected event working fine, but I’m struggling to come up with the logic for an OnUndetected event. Any ideas how i might go about checking when an GameObject is no longer the “CurrentTarget” variable? Maybe something like “CheckLastTarget?”
The bulk of my “detection” code:
if (Physics.SphereCast(transform.position, DetectionRadius, inputDirection, out info, MaxDetection, TargetLayerMask))
{
//Check all Tags
for (int i = 0; i < EnemyTags.Length; i++)
{
//if detected collider has Tag
if (info.collider.gameObject.CompareTag(EnemyTags))
{
if(info.collider.gameObject != Player)
{
//sets collider as current target
currentTarget = info.collider.gameObject;
//NPCHandler Check
if (currentTarget.GetComponent<NPCHandler>() != null)
{
NPCHandle = currentTarget.GetComponent<NPCHandler>();
NPCHandle.OnDetected.Invoke();
}
}
}
}
}