Hi, im new to unity and i was wondering if any one can help me.
I am trying to make a sound audio play from a speaker as the player walks past. So for example the player will walk past the speaker and it will trigger the sound and play.
Does anybody know how this can be done?
Yes 
Are you wanting the sound always to be playing and just get louder as the player approaches and softer as the player moves away? Or do you want the sound to actually ‘switch on’ when the player gets within a certain distance of the source?
Hi. i want the sound to “switch on” as the player gets near to the object.
Add a collider (of appropriate size and shape) to the ‘speaker’ object, and check the ‘trigger’ checkbox. Create a new script, and add it to the ‘speaker’ object. Add an OnTriggerEnter() function to this script. In this function, check to see if the AudioSource component is already playing, and if it’s not, start playback (e.g. using the Play() function).
Note that if the player is not the only object that might interact with the trigger, you may need to check first to make sure the object that’s intersected the trigger is in fact the player. (An easy way to do this would be to check the object’s tag.)
sorry as im new to this i’ve never written a script before could you possibly post like a layout. Like how i would write the list of commands. Thanks.
We can certainly help you write the script.
What programming language are you using? Do you know how to create a new script and add it to a game object?
Yes i know how to create a new java script. and i also know how to add it. but im not sure how to write one.
Ok, the first steps will be:
- Add a collider to the ‘speaker’ object
- Make the object a trigger (check the ‘trigger’ checkbox)
- Create a script with an empty OnTriggerEnter() function and add it to the speaker object
See the docs for an example that you can base your script on. Basically, you can just take the first example there and remove this line:
Destroy(other.gameObject);
ok thanks i will try to do that. A quick question in my unity scene i have a radio and i have already placed my audio source and clip to the object. I want the sound to get quiet as i move further away from it. at the moment it does this but, i cant still here faint music in the background. is there a way to adjust the settings so the sound fades out over a certain distance?
Do you mean you ‘can’ hear faint music in the background?
In any case, read up on the various parameters for the AudioSource component (there’s a variety of parameters you can tweak to control how the sound attenuates).
thanks it worked. As for the triggers sound i will try that tomorrow and see if i have any luck.