private IEnumerator AudioFadeOut( ) { float startTime = Time.time; _source.volume = AudioUtils.DecibelToLinear(startVolume); _source.pitch = startPitch; while (true) { float t = (Time.time - startTime) / fadeTime; float volume = Mathf.Lerp(_audioData.baseVolumeDB, startVolume, t); float pitch = Mathf.Lerp(_audioData.pitchShift, startPitch, t); float linearVolume = AudioUtils.DecibelToLinear(volume); float linearPitch = AudioUtils.SemitoneToPitch(pitch); _source.volume = linearVolume; _source.pitch = linearPitch; if (Mathfs.Approximately(volume, startVolume)) { _source.volume = AudioUtils.DecibelToLinear(startVolume); _source.pitch = AudioUtils.SemitoneToPitch(startPitch); _source.Stop(); break; } yield return(null); } }
private IEnumerator CrossfadeAmbient(AudioSource current, AudioSource next, float fadeTime) { float startTime = Time.time; while (true) { float elapsed = Time.time - startTime; float volume = Mathf.Lerp(-20.0f, maxVolumeDb, elapsed / fadeTime); float linearVolume = AudioUtils.DecibelToLinear(volume); current.volume = 1.0f - linearVolume; next.volume = linearVolume; if (Mathfs.Approximately(volume, maxVolumeDb)) { current.Stop(); next.volume = AudioUtils.DecibelToLinear(maxVolumeDb); break; } yield return(null); } }
private void Update() { if (Mathfs.Approximately(_targetIntensity, _light.intensity)) { // We reached our intensity, pick a new one _targetIntensity = Freya.Random.Range(minIntensity, maxIntensity); } if (Mathfs.DistanceSquared(_targetPos, transform.position) < 0.01f) { // We reached our position, pick a new one var tarX = Freya.Random.Range(_rootPosition.x - posVariance, _rootPosition.x + posVariance); var tarY = Freya.Random.Range(_rootPosition.y - posVariance, _rootPosition.y + posVariance); var tarZ = Freya.Random.Range(_rootPosition.z - posVariance, _rootPosition.z + posVariance); _targetPos = new Vector3(tarX, tarY, tarZ); } _light.intensity = Mathfs.MoveTowards(_light.intensity, _targetIntensity, intensitySpeed * Time.deltaTime); _light.transform.position = Vector3.MoveTowards(_light.transform.position, _targetPos, posSpeed * Time.deltaTime); }