Hi all. I've been trying to get a sound effect to get louder and fainter as you get nearer or further away. For some reason I thought this was possible quite easily, but it seems there is no easy way to add a sound effect to an entity, or I have missed something obvious. So I though, okay I can probably work around it. So far, I have not got it to work though.
This script is what I have come up with so far.
function soundtrigger_init(e)
noise=0
nlevel=0
end
function soundtrigger_main(e)
PlayerDX = g_Entity[e]['x'] - g_PlayerPosX;
PlayerDY = g_Entity[e]['y'] - g_PlayerPosY;
PlayerDZ = g_Entity[e]['z'] - g_PlayerPosZ;
PlayerDist = math.sqrt(math.abs(PlayerDX*PlayerDX)+math.abs(PlayerDY*PlayerDY)+math.abs(PlayerDZ*PlayerDZ));
if PlayerDist < 1000 and noise==0 then
LoopSound(e,0);
noise=1
end
if PlayerDist > 1000 and noise==1 then
StopSound(e,0)
noise=0
end
if noise==1 then
nlevel=PlayerDist/10
SetSoundVolume(nlevel)
end
end
It works exactly the opposite way I want it to. No surprise as it uses the distance to decide how loud to play. I'm struggling to get it to work in reverse, with my efforts failing to play anything or coming up with illegal sound volume levels. I thought all I need do is something like this. Just the idea rather than code.
volume=PlayerDist/10 ( because the initial distance is 1000 and we need 100 max)
newvolume=maxvolume-volume
Probably my newness to Lua causing me headaches. I can't be far away with that, but I couldn't get it going.
SPECS: Q6600 CPU. Nvidia 260GTX. 8 Gig Memory. Win 7.