private bool CanISeeThisThing(ThingToShoot thing) { Vector3 dir = (thing.transform.position - transform.position).normalized; Ray ray = new Ray(transform.position, dir); if (Physics.Raycast(ray, out RaycastHit hit)) { if (hit.transform == thing.transform) { return(true); } } return(false); }
void SetTargetToClosest() { timeUntilUpdatingTarget = delayBetweenUpdatingTarget; // reset timer target = null; float closestDistanceSquared = 0; for (int i = 0; i < possibleTargets.Count; i++) { ThingToShoot thing = possibleTargets[i]; float d2 = (thing.transform.position - visionOrigin.position).sqrMagnitude; if (d2 < closestDistanceSquared || target == null) { if (CanISeeThisThing(thing)) { target = thing.transform; closestDistanceSquared = d2; } } } }