unity-game-engineaudioaudio-source

Audio based on player position in unity


I am working on a unity game, and there is a speaker upstairs in a house playing music. I want the music to get louder as I get closer and quieter as I move further away. I have attached an AudioSource to the object and I have selected force to mono.

I have also set the Rolloff min and max distance to 0.2 and 12. Here is my inspector:

enter image description here

enter image description here

However, for some reason the volume of the music stays the same no matter where I am in the house.


Solution

  • In the AudioSource the value of Spatial Blend ist set to 0 (= 2D). This results in a setting used for a 2D game, where an audio simply gets louder if you get closer.

    In order to get a 3D sound set it to 1 (= 3D). This enables all you are after.

    After doing that you can adjust further how the volume shall falloff with the distance to the object and tweak all the other effects.