x


Adjust audio based on Proximity

How can I adjust audio volume based on how close the player gets to a game object?

Please let me know. Thank you!

more ▼

asked Jun 05 '12 at 07:51 PM

shawnkilian gravatar image

shawnkilian
361 21 28 41

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

define your player's distance from the object and tell it to play when he gets within range:

for instance... something like this would be attached to your game object. Make sure your game object has an audio source on it and a clip attached.. otherwise assign a clip in code. I'll do that here...

var playerDistance : float;
var gameAudio : AudioClip;
var canPlayAudio = true;

function Update() {

playerDistance = player.transform.position - transform.position;

if (playerDistance < 10) {
if (canPlayAudio) {
audio.clip = gameAudio;
audio.volume = 0.5; // adjust the volume to whatever you want
audio.Play();
canPlayAudio = false;  // you can reset this in your code elsewhere... I did this just to make sure the audio only plays once.
}
}
}
more ▼

answered Jun 05 '12 at 09:05 PM

stingman gravatar image

stingman
562 24 29 33

Thank you for the help! I tested this code out also and it say player is not defined as a variable so i added

var player;

and it stopped the errors but i don't think the script is actually doing anything because my sound plays exactly the same when I'm close to the gameobject and when I'm far from the gameobject. Unfortunately Unity's 3D sound isn't giving me the effect I'm looking for. Maybe I'm doing something wrong? Please help if you can. i appreciate it!

Jun 05 '12 at 11:05 PM shawnkilian

yes sorry I should have noted this is just an idea to work off of. Post what you currently have and let me know exactly what you want to happen... specifics and I'll get it working for you. JS or C# is fine

Jun 06 '12 at 05:09 PM stingman
(comments are locked)
10|3000 characters needed characters left

Well, that's the point of a 3D sound. Sounds quite when far from the listener and loud when next to it. But, if you want to do this by script you can add a script to the gameobject which has an audio source and do some coding like:

C# code:

    public Transform target;

    void Start () {

    StartCoroutine(AdjustVolume());

    }

    IEnumerator AdjustVolume () {

    while(true) {

if(audio.isPlaying) { // do this only if some audio is being played in this gameObject's AudioSource

    float distanceToTarget = Vector3.Distance(transform.position, target.position); // Assuming that the target is the player or the audio listener

    if(distanceToTarget < 1) { distanceToTarget = 1; }

    audio.volume = 1/distanceToTarget; // this works as a linear function, while the 3D sound works like a logarithmic function, so the effect will be a little different (correct me if I'm wrong)

    yield return new WaitForSeconds(1); // this will adjust the volume based on distance every 1 second (Obviously, You can reduce this to a lower value if you want more updates per second)

    }
    }

    }
more ▼

answered Jun 05 '12 at 09:05 PM

GutoThomas gravatar image

GutoThomas
593 32 46 47

Thank you for your help! I tried out the C# code and it gives me an error on the first line

public Transform target;

Thoughts?

Jun 05 '12 at 11:03 PM shawnkilian
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x801
x74
x51
x39

asked: Jun 05 '12 at 07:51 PM

Seen: 481 times

Last Updated: Jun 06 '12 at 05:09 PM