I can’t seem to find any documentation on playing audio files.
I did find that you call the sound through the Play() function. I am trying to play a sound, when the player gets close to a game object.
I initially thought, similar to torque, that assigning a game object a audio source would automatically play the sound.
Any ideas or links?
Assigning an audio source will play automatically if playOnAwake is set to true. Otherwise, you manually call Play.
Thanks for the reply.
Manually calling Play() is what I am trying to figure out.
I want to play an audio file (music) when the player enters a specific room.
–
I assume I can attach a script to the player, and whenever he comes within 10 units of a Gameobject I can trigger a sound.
I just can’t find any examples online.
Like…
AudioFilename.audio.Play();
Make the gameObject have an AudioSource on it, and put a sphere collider on it with a 10 unit radius, and make the collider a trigger, then put this script on it:
OnTriggerEnter () {
audio.Play();
}
–Eric
Thanks again…
That is how easy I thought it SHOULD be.
You rock!
–
Just a minor note. If anyone is using this thread…
OnTriggerEnter () {
audio.Play();
}
Should be…
function OnTriggerEnter () {
audio.Play();
}
If you were looking to do this where the script was attached to the player rather than the other object, you would just need to refer to audio for the other object’s attached AudioSource.
You would do something like:
otherObject.audio.Play();
where otherObject is the GO with the attached audio.
In most cases, having the code for playing as part of the object that is doing the playing makes more sense though.
I usually just have all music that’s going to be used for a scene inside a main “player” that has a vector of audio clips and a function that plays the music with the number passed to the function. This also makes it easy to set up a “jukebox menu” for the actual game.