/// <summary> /// Checks to see if the AI has direct line of sight of an object it want s to use as cover /// </summary> /// <param name="coverObject"> Object to be checked for cover </param> /// <returns> True if the AI has direct line of sight </returns> private bool CheckSightOfCoverObject(Transform coverObject) { Vector3 start = GetSightCheckPosition(trans, CoverNormal, CurrCoverType); Vector3 direction = coverObject.position - start; direction.Normalize(); float distance = Vector3.Distance(start, coverObject.position); Ray ray = new Ray(start, direction); RaycastHit hit; if (Physics.Raycast(ray, out hit, distance)) { if (hit.collider.transform.GetInstanceID() == coverObject.GetInstanceID()) { return(true); } else if (hit.collider.gameObject.layer != LayerMask.NameToLayer("Scenery")) { RaycastHit rehit; Vector3 hitPoint = hit.point; float distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f; int iterations = 0; while (MissionInput.RaycastThroughObject(hitPoint, direction, distanceLeft, out rehit)) { if (hit.collider.transform.GetInstanceID() == coverObject.GetInstanceID()) { return(true); } else if (hit.collider.gameObject.layer != LayerMask.NameToLayer("Scenery")) { hitPoint = rehit.point; distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f; iterations++; if (iterations > 50) { break; } continue; } else { return(false); } } return(false); } else { return(false); } } return(false); }
/// <summary> /// Checks whether there is a line of sight between this AI and their target /// </summary> /// <param name="target"> The target of the line of sight check </param> /// <param name="normal"> The normal of the cover of this AI </param> /// <returns> True if there is line of sight </returns> protected bool CheckLineOfSight(AbstractCombatPerson target, Vector3 normal) { Vector3 start = GetSightCheckPosition(trans, normal, CurrCoverType); Vector3 end = GetSightCheckPosition(target.Trans, target.CoverNormal, target.CurrCoverType); Vector3 direction = end - start; direction.Normalize(); float distance = Vector3.Distance(start, end); Ray ray = new Ray(start, direction); RaycastHit hit; if (Physics.Raycast(ray, out hit, distance)) { if (hit.collider.CompareTag(target.tag)) { return(true); } else if (hit.collider.CompareTag(tag)) { RaycastHit rehit; Vector3 hitPoint = hit.point; float distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f; int iterations = 0; while (MissionInput.RaycastThroughObject(hitPoint, direction, distanceLeft, out rehit)) { if (hit.collider.CompareTag(target.tag)) { return(true); } else if (hit.collider.CompareTag(tag)) { hitPoint = rehit.point; distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f; iterations++; if (iterations > 50) { break; } continue; } else { return(false); } } return(true); } else { return(false); } } else { return(true); } }