Search Unity

Audio based on player position

Discussion in 'Scripting' started by Velketor, Oct 21, 2007.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Hello,

    I am new to Unity and I am attempting to have an audio clip fade in and out depending on how close the player is to the object.

    At the moment, I have placed an Audio Source on an object and I have added an audio clip to it. The object currently plays the audio clip at the same volume no matter how close/far away the user is from the object.

    Does anyone know of a script that allows the player to hear the audio clip at full volume when they are near the object...but as the player moves away from the object they will hear less and less of the audio clip...?

    Any suggestions/support is GREATLY appreciated =) Thanks!

    --Velk
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    The Audio Clip has to be in mono for spatialized sounds to work. The other factor is the Rolloff Factor setting for the Audio Source in the Inspector.
     
  3. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Thanks for the quick response, I appreciate it =)

    When you say Mono...do you mean, only being able to hear sound from one of your speakers or...?

    Also, does anyone know if I can make an audio track save in "Mono" format in Soundtrack Pro?

    Thanks again =)

    --Velk
     
  4. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Yes, as in the left and right channels mixed together into one channel.

    If not, "Audacity" will mix and save to one track and is free...
     
  5. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    A mono recording is a recording with a single track of audio, so you only see one waveform in the sample editor. This means it can be played back from a single point.

    Stereo adds a second track -- a second waveform -- in the sample editor. The waveforms correspond to the audio emitted from each of two speakers: a left speaker and a right speaker. This is how most music is recorded and is what most headphones support.

    Additional tracks are normally used to provide surround sound. E.g. the "5.1" format uses five audio tracks, one each for front-left, front-right, front center (usually for speech), rear-left and rear-right speakers. The additional ".1" waveform is for the subwoofer and contains only the bass (i.e. low-frequency) audio.

    Unity requires that all audio you wish to be positioned within a Scene be in mono format. This allows Unity to mix it, pan it and handle roll-off in real-time. Stereo audio is never played back like this; it's played back exactly as you would expect to hear it were you playing it back in iTunes or Soundtrack Pro.

    Yes: use the Convert to Mono function in the Process menu.

    (NOTE: I have Soundtrack Pro 2.0.1, which came bundled with Logic Studio; if you have an older version, it may not be in the same place.)

    Regards,
     
  6. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Wow!

    Thanks so much for your advice! I will try it out A.S.A.P and let you know if it worked =)

    Thanks!

    --Velk
     
  7. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Here is what's happening now...

    So I added an "Audio Source" in MONO format to an object and turned the roll-off factor up and down several times to test it out...but after many attempts messing around with the different settings in the audio source options, the volume of the Audio Source only became lower OVERALL--not when the player goes near the object or away from it.

    Does anyone know if I am forgetting a step?

    Here is a refresher on what I am trying to do:

    I want an audio track to play at a louder volume as my player walks CLOSER to an object...but if the player walks away from the object, the volume should get lower.

    Any help is appreciated! Thanks!

    --Velk
     
  8. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    This seems odd. The "Roll Off" value dictates how far away you have to be before you hear the sound. Note that you should also ensure that the "Minimum Volume" and "Maximum Volume" settings are sensible: if you set them both to the same value, there won't be any rolloff. (Similarly, you don't want to invert them: if the minimum value is higher than the maximum, I think Unity will just clamp to the loudest of the two.)

    You need to set the roll-off value to a relatively high value -- e.g. 1.0 -- if you want it to fade quickly into the distance. Use lower values, such as 0.2, if you want the sound to fade away over a greater distance. The Unity docs do cover this.

    Regards,
     
  9. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Thanks for the quick response =)

    Unfortunately, I am still having the same problem. I have adjusted the Audio Source settings numerous times (even following your directions) and I am still not seeing results =(

    Any other suggestions? I'm stuck :x
     
  10. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    Hmm.

    I know you recorded the sample as a mono file, but have you checked Unity's import settings for it? You need to ensure that the "Channels" setting is on "Force Mono", regardless of the file itself. (I also recommend using "Ogg Vorbis" and "Decompress On Load" if you're building for the Web.)

    Note that you do need to reimport changed files. If you simply drag-dropped it onto Unity back when it was a stereo file and never touched it afterwards, Unity itself is probably assuming it's still a stereo file.

    Regards,
     
  11. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Thanks for the quick response =)

    Ok,

    So I searched the forums, the Wiki and Unity's menus but I have had no luck finding the "Import Settings" or the "Channels" or the "Force Mono"...

    Call me a n00b (which I am lol) but where can I find the Import Settings option?

    I would greatly appreciate your help and support! Thank you!

    --Velk
     
  12. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Right click the sound file (or any asset for that matter) and click on "import settings" in the pop-up menu.
     
  13. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Wow, quick response =) Thank you!

    Your advice has taken me one step closer...but...

    Although I am able to see the "Import Settings" option located in the pop-up menu when I right-click the sound file...it appears to be greyed out (as in, I cannot use it for some reason).

    Is there any way I can make the option usable?

    Any help is GREATLY appreciated =)

    Thank you!

    --Velk
     
  14. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    Sounds like Unity doesn't know where the original asset is any more. Try deleting the asset form the Project Pane and copying the new (mono) asset back in. Then try the "Reimport..." trick again to ensure it's set to Force Mono, etc.

    (If it's still greyed-out, scream for a few seconds, then run some system tests on your computer. It may be that your hard disk needs its permissions repaired. Or something.)
     
  15. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    And to be perfectly clear, as per the docs you do have an Audio Listener attached to your main camera, right? With an Audio Listener attached to your camera your Mono audio sources will automatically have spacial effects (left/right pan, attenuation with distance) applied.
     
  16. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    I never had an Audio Listener attached to the camera...never knew I had to =p

    I'll try it out and get back to you =)

    Thanks for the information everyone, this has cleared up a lot for me!

    --Velk