Forum Discussion
JaggerWolf
11 years agoExpert Protege
Need Audio Help In Unity!!
I have been designing a game level in Unity 4 - That part was easy....Now I am onto adding sounds for the level. I suck at scripts, still haven't got the hang of it. I am looking for a way to trigger a sound that is on an object away from the player - I am guessing I would need two scripts one for the trigger and one on the object where the sound is.
Example: Player walks through trigger - The sound plays behind them, Or - Player enters trigger - sound turns on the radio on the shelf.
I have been able to make a trigger with a sound attached, but have been unable to trigger a sound on an object that is a distance from the player.
Any help would be great - I have asked on unity forums but there hasn't been much help.
Thanks!!
Example: Player walks through trigger - The sound plays behind them, Or - Player enters trigger - sound turns on the radio on the shelf.
I have been able to make a trigger with a sound attached, but have been unable to trigger a sound on an object that is a distance from the player.
Any help would be great - I have asked on unity forums but there hasn't been much help.
Thanks!!
2 Replies
Replies have been turned off for this discussion
- moltonExplorerI think you want to make a reference to that object with the sound from your trigger script to play the sound. something like
Gameobject the_radio;
the_radio = Gameobject.Find("the_radio_gameobject");
the_radio.audio.Play(); - bilagoHonored Guest
"JaggerWolf" wrote:
I have been designing a game level in Unity 4 - That part was easy....Now I am onto adding sounds for the level. I suck at scripts, still haven't got the hang of it. I am looking for a way to trigger a sound that is on an object away from the player - I am guessing I would need two scripts one for the trigger and one on the object where the sound is.
Example: Player walks through trigger - The sound plays behind them, Or - Player enters trigger - sound turns on the radio on the shelf.
I have been able to make a trigger with a sound attached, but have been unable to trigger a sound on an object that is a distance from the player.
Any help would be great - I have asked on unity forums but there hasn't been much help.
Thanks!!
This is how I manage my sound effects:
/// <summary>
/// Play an Effect from a GameObject with an audiosource
/// </summary>
/// <param name="sourceObject">Object with AudioSource</param>
/// <param name="clip">Clip you want to play</param>
/// <param name="multiplier">Percent of max volume do you want this to play at (ie 0.5f = 50%)</param>
public void playEffect(GameObject sourceObject, AudioClip clip, float multiplier)
{
if (sourceObject.GetComponent<AudioSource>() != null)
{
sourceObject.GetComponent<AudioSource>().PlayOneShot(clip, masterVolume_Effects * multiplier);
Debug.Log(string.Format("Sound {0} has been played by Object {1}", clip.name, sourceObject.name));
}
else
Debug.Log(string.Format("Object {0} does not have an audio source!", sourceObject.name));
}
Put that in your script, and ontriggerEnter() you can then callplayEffect(this.gameObject,clipToPlay,0.5f);
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 9 months ago
- 4 years ago
- 6 years ago