/// <summary> /// Finds all targetable objects in the scene. NOTE: this is expensive; don't do every frame! /// Anything too far away or outside the player's FOV is ignored and not included in the set /// of possible targets. /// </summary> void ScanSceneForTargets() { timeUntilScan = delayBetweenScans; // reset timer ThingToShoot[] targets = FindObjectsOfType <ThingToShoot>(); possibleTargets.Clear(); float fovThreshold = AnimMath.Map(viewFOV, 0, 180, 1, 0); foreach (ThingToShoot target in targets) { Vector3 dis = target.transform.position - visionOrigin.position; bool tooFarAway = (dis.sqrMagnitude > viewDistance * viewDistance); bool outsideOfFOV = (Vector3.Dot(transform.forward, dis.normalized)) < fovThreshold; if (!tooFarAway && !outsideOfFOV) { possibleTargets.Add(target); } } //print(possibleTargets.Count); }