Sound's volume set dynamically depending on closeness to player character

Started by bx83, Sat 01/02/2020 22:43:29

Previous topic - Next topic

bx83

You set a max/min-volume for a position; the closer the player character gets to this position, the higher the volume is; and the further away is a downshift in volume.

Does anyone know if a sound function like this exists? I seem to remember seeing something like it a couple of years ago, but not sure where it is or exactly what it entails.

And 99% of you will say 'check tween', but do you know if anything like this exists in AGS?

ManicMatt

Someone will likely come up with something better, but id imagine you can code in repeatedly execute to change the volume depending on the x and y co-ordinates of the character? But would need to use greater than and less than for the co-ordinates.

I'm really bad at explaining stuff and this is just hypothetical so please wait for someone to tell me this is a terrible suggestion lol.

I thought I'd take a stab anyway.

eri0o

There is AudioChannel.SetRoomLocation

https://adventuregamestudio.github.io/ags-manual/AudioChannel.html#audiochannelsetroomlocation
Quote
Code: ags
AudioChannel.SetRoomLocation

(Formerly part of PlayAmbientSound, which is now obsolete)

AudioChannel.SetRoomLocation(int x, int y)
Sets the currently playing audio to be a directional sound, emanating from (x,y).

The volume of the channel will be dynamically adjusted depending on how close the player character is to the co-ordinates. Therefore, as the player walks closer the volume will increase, and as they walk away the volume will decrease.

The channel's Volume setting sets the maximum possible volume when the player is standing on the specified co-ordinates.

Pass the co-ordinates as (0,0) to remove the directional effect and return this channel to playing at its normal volume.

Example:

Code: ags
AudioChannel *channel = aMachine.Play();
channel.SetRoomLocation(oMachine.X, oMachine.Y);

will start playing the aMachine audio clip, and set it at the location of the oMachine room object.

If you want to control max/min volume and panning, it's better if you write your own function to do this, just calculate the player character distance to your point of interest and use this to manipulate volume (and panning if desired) on room rep execute or repeatedly execute always, and only operate if your audio channel is not null. The audio channel is set from audio clip on an event, like room load, check the audioclip for null returns too if you want to prevent a crash when no channel is available.

ManicMatt

Oh, nice! I'm gonna keep this in mind! I wasn't far off in principle, I didn't know there was a built in feature like this!

SMF spam blocked by CleanTalk