I'm attempting to use a Lua trigger to change the properties of a sound emitter based on the car's speed as it passes through. The general logic is working fine, but the volume and pitch changes are only being applied while the world editor is open. Code: local emitter = scenetree.findObjectById(scenetree.SoundGroup.obj:idAt(0)) -- # of corresponding sound emitter local function emitSound(data) if data.event == 'exit' then-- mute if exit emitter.volume = 0 end if data.event == 'enter' then --play sound when enter trigger local veh = scenetree.findObject(data.subjectName) -- get triggering vehicle local speed = vec3(veh:getDirectionVector()):dot(vec3(veh:getVelocity(veh:getRefNodeId()))) -- get car velocity emitter.pitch = (speed / 11.11) -- scale pitch based on velocity emitter.volume = (0.0288 * speed) - 0.2 + 1.2 -- scale volume based on velocity end end return emitSound
If it's an SFXEmitter, you'll have to delete it and create it again. You can't change the volume after it has been created sadly. For my flood mod, I do this: Code: local function setRainVolume(vol) local soundObj = findObject("rain_sound") if soundObj then soundObj:delete() end soundObj = createObject("SFXEmitter") soundObj.scale = Point3F(100, 100, 100) soundObj.fileName = String('/art/sound/environment/amb_rain_medium.ogg') soundObj.playOnAdd = true soundObj.isLooping = true soundObj.volume = vol soundObj.isStreaming = true soundObj.is3D = false soundObj:registerObject('rain_sound') end I'm hoping they make it so you can change the sound after creation. Maybe one day..
I found a fix, turns out that you can! Just needed to add Code: emitter:postApply() in this case: Code: local emitter = scenetree.findObjectById(scenetree.SoundGroup.obj:idAt(0)) local function emitSound(data) local veh = scenetree.findObject(data.subjectName) local speed = vec3(veh:getDirectionVector()):dot(vec3(veh:getVelocity(veh:getRefNodeId()))) emitter.pitch = math.max(speed * .09, 0.1) emitter.volume = math.max(0.0288 * speed - 0.2, 0) emitter:postApply() if data.event == 'exit' then emitter.volume = 0 emitter:postApply() end end return emitSound