public void findRangeForSound() { bool playerFound = false; GameObject closestObject = null; List <GameObject> targets = detector.getNearByObjects(); if (targets.Count > 0) { closestObject = targets[0]; float closestDist = Vector3.Distance(closestObject.transform.position, this.transform.position); foreach (GameObject current in targets) { if (current.tag == "Player") { playerFound = true; source.pitch = Random.Range(lowPitchRange, highPitchRange); float Vol = Vector3.Distance(current.transform.position, this.transform.position) * distToVol; source.PlayOneShot(Behemoth_Growl_Low_Groam_01, Vol); //source.Play(); } } } else { playerFound = false; } if (playerFound == false) { source.Stop(); } }
//uses the objects found by the animal's detector object and finds the closest one //If it finds a player it will always be the closest object regardless. //So animals will join teams to fight the player public GameObject findClosestTarget() { GameObject closestObject = null; List <GameObject> targets = detector.getNearByObjects(); if (targets.Count > 0) { //assumes the closest object is the first one closestObject = targets[0]; float closestDist = Vector3.Distance(closestObject.transform.position, this.transform.position); foreach (GameObject current in targets) { //if it's the player it will always take priority if (current.tag == "Player") { return(current); } float distance = Vector3.Distance(current.transform.position, this.transform.position); if (distance < closestDist) { closestObject = current; closestDist = distance; } } } return(closestObject); }