How to make sound get louder if enemy close

Hey. How i would make my background music get louder when my enemy starts to chase me. I really have no idea so any info will be appreciated thanks.

You want to look at the Transform position for your player and your enemy. Take the Vector3.distance between the two transforms, and set the AudioSource volume based on this distance. In reality, volume falls off exponentially rather than linearly, so take that into account. If you simply want to switch the music louder (on/off) when being chased, you can use something like this:

if (Vector3.Distance(playerTransform.position, enemyTransform.position) < chaseDistance)
  audio.volume = 1f;
else
  audio.volume = 0.5f;

(1f and 0.5f are C# syntax - if you’re using JS drop the f)

One way to do it is to attatch the same music to the camera and enemy and make it play at same time (make sure its looping and playing auto) and make the volume playing louder at the enemy so when its close the sound become louder

Another way to do this is to attatch this script to your ai so when its see u add this line
AudioListener.volume = hSliderValue/10; }

//SLIMEBASS