Created
February 20, 2026 14:25
-
-
Save bsimser/06c7d9d8bce83c95e9cbae4a7b854bac to your computer and use it in GitHub Desktop.
For anyone who would like the enemy to be able to hear you, place the script on your enemy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /////////////////////////////////////////////////////////////////////////////// | |
| // | |
| // This code is licensed under MIT license. | |
| // | |
| // Copyright © 2026 Bil Simser, https://www.simstools.com | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy | |
| // of this software and associated documentation files (the "Software"), to | |
| // deal in the Software without restriction, including without limitation the rights | |
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| // copies of the Software, and to permit persons to whom the Software is | |
| // furnished to do so, subject to the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be included in | |
| // all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| // THE SOFTWARE. | |
| // | |
| /////////////////////////////////////////////////////////////////////////////// | |
| using UnityEngine; | |
| using UnityEngine.Events; | |
| /////////////////////////////////////////////////////////////////////////////// | |
| /// | |
| /// For anyone who would like the enemy to be able to hear you, place the script on your enemy. | |
| /// soundSources is your player prefab (hero player), and other things that produce sounds. | |
| /// OnSoundHeardEvent specifies what should happen when the enemy hears you. | |
| /// | |
| /////////////////////////////////////////////////////////////////////////////// | |
| public class EnemySoundListener : MonoBehaviour | |
| { | |
| [SerializeField] private GameObject[] soundSources; | |
| [SerializeField] private float hearingDistance = 10f; | |
| [SerializeField] private float hearingAngle = 90f; | |
| [SerializeField] private UnityEvent onSoundHeard; | |
| private AudioSource[] soundSourceAudios; | |
| private void Start() | |
| { | |
| soundSourceAudios = new AudioSource[soundSources.Length]; | |
| for (int i = 0; i < soundSources.Length; i++) | |
| { | |
| soundSourceAudios[i] = soundSources[i].GetComponent<AudioSource>(); | |
| } | |
| } | |
| private void Update() | |
| { | |
| if (CanHearSound()) | |
| { | |
| Debug.Log("Heard sound!"); | |
| onSoundHeard.Invoke(); | |
| } | |
| } | |
| private bool CanHearSound() | |
| { | |
| if (soundSources == null || soundSources.Length == 0) | |
| { | |
| return false; // No sound sources assigned, cannot hear anything | |
| } | |
| foreach (var soundSourceAudio in soundSourceAudios) | |
| { | |
| if (soundSourceAudio == null) | |
| { | |
| continue; // Skip null audio sources | |
| } | |
| if (!soundSourceAudio.isPlaying) | |
| { | |
| continue; // Sound source audio is not playing | |
| } | |
| Vector3 directionToSoundSource = soundSourceAudio.transform.position - transform.position; | |
| float angleToSoundSource = Vector3.Angle(transform.forward, directionToSoundSource); | |
| if (directionToSoundSource.magnitude <= hearingDistance && angleToSoundSource <= hearingAngle / 2f) | |
| { | |
| // Sound source is within hearing distance and angle | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment