/// <summary> /// Get all objects matching the required criteria within a sphere centered on the agents position. /// </summary> /// <param name="centre">The center point of the spehere.</param> /// <param name="radius">The radius of the sphere.</param> /// <param name="includeFilter">A filter that identifies results that should be included in the results.</param> /// <param name="result">All colliders within the sphere that match the filter</param> public static List <Collider> GetObjectsWithinSphere(Vector3 centre, float radius, ObjectFilter includeFilter) { List <Collider> result = new List <Collider>(); Collider[] hitColliders = Physics.OverlapSphere(centre, radius); for (int i = 0; i < hitColliders.Length; i++) { if (includeFilter.gameObject != null && GameObject.ReferenceEquals(includeFilter.gameObject, hitColliders[i].gameObject)) { result.Add(hitColliders[i]); } else if (!string.IsNullOrEmpty(includeFilter.tag)) { if (hitColliders[i].CompareTag(includeFilter.tag)) { result.Add(hitColliders[i]); } else { result.Add(hitColliders[i]); } } } return(result); }
/// <summary> /// Find all objects matching a filter that are within Line of Site. /// </summary> public static List <Transform> FindVisibleTargets(Transform viewer, Vector3 eyesPosition, float radius, float fovAngle, LayerMask obstacleMask, ObjectFilter filter) { List <Transform> visibleTargets = new List <Transform>(); if (radius == 0) { return(visibleTargets); } List <Collider> targetsInViewRadius = GetObjectsWithinSphere(eyesPosition, radius, filter); for (int i = 0; i < targetsInViewRadius.Count; i++) { Transform target = targetsInViewRadius[i].transform; Vector3 dirToTarget = (target.position - eyesPosition).normalized; if (Vector3.Angle(viewer.forward, dirToTarget) <= fovAngle / 2) { float distanceToTarget = Vector3.Distance(eyesPosition, target.position); if (!Physics.Raycast(eyesPosition, dirToTarget, distanceToTarget, obstacleMask)) { Debug.Log(viewer + " can see " + target); visibleTargets.Add(target); } else { Debug.Log(viewer + " can't see " + target); } } } return(visibleTargets); }