void FindVisibleTargets() { //Clear the current visible targets visibleTargets.Clear(); //Do simple sphere collision check for nearby targets Collider[] targets = Physics.OverlapSphere(transform.position, ViewRadius, TargetLayer); //Iterate through each target foreach (Collider target in targets) { //Get direction and magnitude to target Vector3 ToTarget = (target.transform.position - transform.position); //Normalize so we have direction without magnitude Vector3 ToTargetNormalized = ToTarget.normalized; if (Vector3.Angle(transform.forward, ToTargetNormalized) < ViewAngle / 2 && //Check if the target is within our FoV !Physics.Raycast(transform.position, ToTargetNormalized, ToTarget.magnitude, ObstacleLayer) && // then do the raycast to determine LoS target.gameObject.tag == "Player" || target.gameObject.tag == "Enemy_AI" && // and check if the object has the right tag target.gameObject != thisObject) // and that it is not the object this script is attached to { //I see you! visibleTargets.Add(target.transform); } } //Add memory record to our perception system Perception percept = GetComponent <Perception>(); percept.ClearFoV(); foreach (Transform target in visibleTargets) { percept.AddMemory(target.gameObject); } }