void FindTargets() { visibleTargets.Clear(); // Avoid duplicates of targets // Store all targets within viewing radius using the target layer mask Collider[] targetsInRange = Physics.OverlapSphere(transform.position, radius, targetMask); // Loop through the list of targets and check if they are in view foreach (Collider col in targetsInRange) { Transform target = col.transform; Vector3 targetDirection = (target.position - transform.position).normalized; if (Maths.AngleInDegrees(transform.forward, targetDirection) < angle / 2) { float targetDistance = Vector3.Distance(transform.position, target.position); if (!Physics.Raycast(transform.position, targetDirection, targetDistance, obstacleMask)) { // The target is in view and not blocked by an obstacle visibleTargets.Add(target); } } } }