I need help reproducing the natural delay of sound.
Example: A sniper is shooting at the player from 1000m away, and due to the speed of sound, there is a roughly 3 second delay for the gunshots. How can I do it?
Mmmpies’ comment explains how to do it. Take the distance of the player and the sniper, then divide by the speed of sound. Delay playing the sound by that value.
//pseudo
float delay = Vector3.Distance(player.transform.position, sniper.transform.position) / 334f;
StartCoroutine(PlaySound(delay));
IEnumerator PlaySound(float delay)
{
yield return new WaitForSeconds(delay);
audioSource.Play();
}
It may not sound right, I would expect alot more echo from a sound from that far. Not sure how you would accomplish that.
OK so you need to record the time the bullet was fired.
fireTime = Time.time;
that’s a float in your script as in private float fireTime;
then call a function to get the delay by passing the transform of player and sniper.
private float DelayTime(Transform player, Transform sniper)
{
return Vector3.Distance(player, sniper) / 334.0f;
}
Then in Update check if Time.time > fireTime + the output from DelayTime. If so play the sound.
Thank you guys for the help, I’ll try and see how it works. I love this community.