# Audio level increases/decreases with proximity to an object

I’m trying to figure out how to write an expression that would produce the following effect in a platformer or top-down game: an object in the scene produces a constant noise (a running motor, a waterfall, etc). When the player is far from this object, the noise should be inaudible/faint, and smoothly increase in volume as the character gets closer, then decrease as the player moves away from it. I’m not sure if a linear or logarithmic fade would work better, I would like to try both. Has anyone here done this, or have any ideas?

Have you tried setting the volume equal to “abs(Motor.X() - Player.X())”? Just a guess.

3 Likes

Thanks, I’ll play around with that and report back.

You are right this is a good solution the first time i wanted this effect i used several hitboxes to lower the volume but what you said gives a smooth transition

2 Likes

Thanks piyushpalawat99, your solution does work, but it only works in one direction so far. If the player is to the left of the noisemaker, it gets louder as they approach it, moving right. However, if they keep going right, it stays at full volume (the goal is for it to get quieter after passing the object). I’m still working on this, and if you have any suggestions in the meantime I would love to hear them! I think this is the way to go, but I haven’t dialed it in yet.

I also tried another approach, using distance between two objects. One variable is volume, the other is distance. Creating an action where volume follows distance works great, just divide distance by an integer to find a ratio between distance/volume that makes sense. For example, divide by 8 and the volume (1-100) changes 12.5% for every 100 pixels.

The only problem is, it’s the exact opposite of what I’m trying to do - sound gets louder as the player walks away, becomes quieter as they approach. It seems like this could work if I could invert those two values. Essentially, can the volume variable be increased in proportion to the distance variable decreasing? I may be going in the wrong direction, but check out this screenshot of the relevant part of the event sheet.

Thanks for taking a look, I really appreciate the support, and I hope this will help others once I get it working!

Have you used “abs()”? That should decrease the volume as you move on the right side.
For the second approach, try using something like “100 - (distance/8)”.

1 Like

Ok, that makes sense… absolute. I did use abs(), but I might have messed up the syntax down the line, I’ll try it again. I’m confident this will be working by tomorrow, thank you so much for your guidance!

1 Like

Thanks piyushpalawat99, got it working! For anyone interested, the expression is:

Do = 100-Variable(Distance)/8 to scene variable Volume

2 Likes

Hey @RSK1,
Just curious, if you tried using the method involving “abs”. And if you did, did you encounter any issues? What were the issues?

1 Like

I’m going to do that next and post the results (before I mark this solved)

1 Like

Alright, it works, but with 1 sound only. How can I make same evemts, but with multiple sounds?