Is there a way to simulate speed of sound delays? Like if there is an explosion a mile away, is there a way I can make it so that the sound happens slightly after you see the explosion? Or would it just have to be something I simulate with a script that measures the distance between the listener and source?
Just make it a 3D sound:
If the Sources are 3D (see import settings in Audio Clip), the Listener will emulate position, velocity and orientation of the sound in the 3D world (You can tweak attenuation and 3D/2D behavior in great detail in Audio Source)
(From the Sound page.)
If you need to handle it differently, start a coroutine that delays the appropriate amount of time.
First you need a function that computes the sound delay between two objects. Something like (see Speed of Sound):
float SoundDelay(Transform a, Transform b) {
float speed = 331.4f + 0.6f * temperatureCelsius;
return Vector3.Distance(a, b) / speed;
}
If you only have one listener (e.g., the player), you could do this:
void IEnumerator Explode(Transform listener) {
animation.Play("Explosion");
float soundDelay
yield return new WaitForSeconds(soundDelay);
audio.Play();
}
If you need to broadcast it to multiple listeners, you can use BroadcastMessage(). to tell every component to delay a certain time and then play the explosion sound.
Or, if you want to get overly complicated, you could create a collider that expands over time. Every listener can handle the collision with the “sound wave” collider using OnColliderEnter().
would it just have to be something I simulate with a script that measures the distance between the listener and source?
Yes.