Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Playing background music.

Discussion in 'Scripting' started by rjdfhorn2006, Oct 15, 2010.

  1. rjdfhorn2006

    rjdfhorn2006

    Joined:
    Jun 14, 2010
    Posts:
    141
    What is the quickest way to play background music but NOT AS A 3D sound. I want the sound level to stay constant throughout the game while my player roams the world freely.
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Set up an audio source on the object with your audio listener. Have that source handle all the music for the game, and it should have a constant volume in all speakers.

    You can also set up an audio source somewhere in the world with a rolloff factor of 0. You'll run into it shifting from speaker to speaker depending on where it is relative to you, though. Could be cool if you attached it to something like an ingame radio or TV....
     
    Wandersoul and Taloose like this.
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Can you just import the respective audio files with '3D Sound' off?
     
  4. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Atatch a streio audio to the main camera, in the same position in the world as the camera. Easy.
     
  5. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Is there a reason that importing the audio file with '3D Sound' set to 'off' wouldn't be a viable solution? (There very well may be - just curious, really.)
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Even if 3D Sound is off, that doesn't stop it from getting quieter as it gets further away due to the rolloff factor. As a result, it wouldn't help if the audio source wasn't attached to the camera.

    It is a solution, yes, but not one that's perfect.
     
  7. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Are you sure about that? Have you actually tried it?

    I just placed a looping sound in a scene with the first-person controller prefab, and the pan and volume of the sound changed as expected as I moved around. I then re-imported the clip with '3D Sound' set to off. After that, the sound played at exactly the same volume regardless of where the listener was in the scene - no attenuation whatsoever. (I tried this in both 2.x and 3, and the results were the same.)

    So again, are you sure that what you're saying is correct?
     
  8. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    After looking into this a bit more, I'm thinking your understanding of what the '3D Sound' option does is likely incorrect.

    As I mentioned, I tried this in both Unity 2.6 and Unity 3, and in each case setting '3D Sound' to 'off' did in fact disable volume attenuation. What would the point of the '3D Sound' option be if it didn't disable volume attenuation? To disable pan and/or doppler only? That would seem kind of odd.

    So again I'll ask, have you actually tried this? Is what you posted above based on actual observation?
     
  9. RickHurd

    RickHurd

    Joined:
    Feb 7, 2013
    Posts:
    1
    I know this thread is a bit old, but for future reference this is what i did.

    1. Make a camera (or use the main camera/ camera you already have setup)
    2. Add "Audio Listener" and "Audio Source" components to the camera
    3. On the Audio Source component add your music.

    The camera can now be the object that can hear the music it plays. The camera will always be at the same spot so you wont have any weird volume issues with distance from the source.
     
    Wiatagan likes this.
  10. gaps

    gaps

    Joined:
    Jan 1, 2014
    Posts:
    15
    The correct way to play sound that does not relate to a world position is to do what Jesse Anders said multiple times above: Select the sound asset, disable the 3D Sound option and apply the changes.
    Having the sound playing at the same position as the camera is just an unnecessary hack and adds complexity where it is not needed.
     
    klesun likes this.