The question is answered, right answer was accepted
Distance delay for sound
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?
$$anonymous$$ake it a 3D sound and it should handle it for you although you might want more control than that.
Where are you playing the sound, is it attached to your player or the sniper?
If the player then it won't make any difference as the distance is zero at all times.
Answer by Jessespike · Jun 15, 2016 at 09:08 PM
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.
Answer by Mmmpies · Jun 15, 2016 at 09:07 PM
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.
I'm most likely doing it wrong, but it doesn't seem to work.
@$$anonymous$$mmpies sorry for bothering you again, but I did as you said and it tells me DelayTime needs to declare a body and I'm not sure what to do.
You need to call the function with 2 transforms, one from your player and the other from the sniper.
On my phone so difficult to give example without typos.
@$$anonymous$$mmpies Well, I did.
using UnityEngine;
using System.Collections;
public class delay : $$anonymous$$onoBehaviour {
private float fireTime = Time.time;
private float DelayTime (Transform player, Transform sniper);
void Update()
{
{
return Vector3.Distance (player, sniper) / 334.0f;
}
if(Time.time>fireTime+out<DelayTime>);
{
AudioSource.Play()
}
}
}
Blimey I'd need a couple of pages to cover the mistakes in that code :D
using UnityEngine;
using System.Collections;
public class delay : $$anonymous$$onoBehaviour {
private float fireTime;
private float delayTime;
private bool played = false;
public Transform playerTransform;
public Transform sniperTransform;
private float DelayTime (Transform player, Transform sniper)
{
return Vector3.Distance(player.position, sniper.position) / 334.0f;
}
void Start()
{
fireTime = Time.time;
delayTime = DelayTime (playerTransform, sniperTransform);
Debug.Log ("fireTime = " + fireTime);
Debug.Log ("delayTime = " + delayTime);
}
void Update()
{
if(Time.time >= (fireTime + delayTime) && !played)
{
Debug.Log ("Playing Sound at " + Time.time);
played = true;
}
}
}
I've left the player and sniper public, all you need is a scene and two objects, I just used the camera and a cube. I moved the cube 1000 away and the delay is about 3 seconds (which is right).
Now for some of the mistakes...
You called Time.time when declaring the variable floatTime that's outside of the game running so won't work correctly.
You set that DelayTime function as a variable rather than a function.
You gave the return value from that function in Update which will never work.
I'll take the hit for not putting player.position and sniper.position in that Distance command but you should really do some of the scripting tutorials as there were several other mistakes in there as well.
Look on YouTube and see if you can find a C# unity tutorial - bound to be some.
Bloody hell, knew it. $$anonymous$$ine was way too simple.
@$$anonymous$$mmpies By the way, thanks a lot.
That's O$$anonymous$$ but do you understand what's happening?
The main part is we have a float delayTime (lower case d) and we set that by getting the return value from the function DelayTime.
We have to call that function by passing it two transforms and rather than being a void function it's a float which means it returns a float.
That float is the distance between two objects (the transforms we sent it positions) divided by 334.
Distance outputs a float which in my case was 1000 when divided by 334 = 2.99 something. And that how long it waits.
It looks complicated but when you break it down it's not that bad.
Alright post your code and I have a look when I can.
EDIT - actually it might be simpler than that. Have you unticked "Play On Awake" on the audiosource?
Answer by Dismantler69 · Jun 15, 2016 at 09:13 PM
Thank you guys for the help, I'll try and see how it works. I love this community.
Follow this Question
Related Questions
Fix sound delay 6 Answers
How do I delay a function to match the duration of a sound clip? 0 Answers
Parsing error CS8205,Parsing Error CS8205 2 Answers
How to play a sound ONCE when the gameObject tagged as , "Player" enters the trigger ? 0 Answers
What causes the audio clicking & how do I get a clean sound? 11 Answers