/// <summary> /// This is to search for targets within our radius /// </summary> void SearchForTarget(GameObject obj) { //We have a reference to someone inside of our radius if (currentTarget) { //Checking to see if they are actually within our radius if (_sphereCollider.bounds.Contains(currentTarget.GetComponent <Collider>().bounds.center)) { OnTargetClearLOS?.Invoke(currentTarget); OnTargetTracking?.Invoke(currentTarget); } } }
private void OnTriggerEnter(Collider other) { // We detect a pickup in our radius if (other.gameObject.tag == TagStatics.GetPickupTag()) { if (InLineOfSight(other.gameObject)) { OnTargetClearLOS?.Invoke(other.gameObject); OnTargetInRadius?.Invoke(other.gameObject); _isTrackingTarget = true; } else { OnTargetInRadius?.Invoke(other.gameObject); _isTrackingTarget = false; } } if (other.gameObject.tag == TagStatics.GetMobTag() && other.gameObject.name == TagStatics.GetPlayerName()) { //The mob is within our LoS if (InLineOfSight(other.gameObject)) { OnTargetClearLOS?.Invoke(other.gameObject); OnTargetInRadius?.Invoke(other.gameObject); _isTrackingTarget = true; } else { OnTargetInRadius?.Invoke(other.gameObject); _isTrackingTarget = false; return; } currentTarget = other.gameObject; } //We heard a noise if (other.gameObject.tag == "Audible") { OnHeardSound?.Invoke(other.gameObject); } }
/// <summary> /// This is to check if we have an unobstructed LoS to whoever enters the monster's search radius /// If it's unobstructed, we know that we can "see" them /// </summary> private bool InLineOfSight(GameObject obj) { var startPos = gameObject.transform.position; var endPos = (obj.transform.position - gameObject.transform.position); RaycastHit hit; if (Physics.Raycast(startPos, endPos, out hit) == true) { if (hit.collider.gameObject.name == obj.name) { OnTargetClearLOS?.Invoke(obj); OnTargetInRadius?.Invoke(obj); Debug.DrawRay(startPos, hit.point - startPos, Color.green); return(true); } else { OnTargetInRadius?.Invoke(obj); return(false); } } return(false); }