C# 应用低通滤波器一段时间,然后将其恢复正常?
我正在尝试实现FPS游戏中被手榴弹击中时的常见效果。。当你被击中时,一个LP滤波器以低频启动,等待几秒钟,然后将其恢复正常。 这种效果是如何产生的?我用的是C和Unity 5。 我试着用谷歌搜索它,但关于这个主题的信息似乎很少 到目前为止,我的代码并不是超级性感,我已经连续删除了250次,我开始对自己失去耐心:pC# 应用低通滤波器一段时间,然后将其恢复正常?,c#,audio,lowpass-filter,C#,Audio,Lowpass Filter,我正在尝试实现FPS游戏中被手榴弹击中时的常见效果。。当你被击中时,一个LP滤波器以低频启动,等待几秒钟,然后将其恢复正常。 这种效果是如何产生的?我用的是C和Unity 5。 我试着用谷歌搜索它,但关于这个主题的信息似乎很少 到目前为止,我的代码并不是超级性感,我已经连续删除了250次,我开始对自己失去耐心:p using UnityEngine; using System.Collections; public class DamageHandler : MonoBehaviour {
using UnityEngine;
using System.Collections;
public class DamageHandler : MonoBehaviour {
[Range(5000, 22000)] public int standardLpFreq = 22000;
[Range(10, 500)] public int hitLpFreq = 280;
public float timeToHaveEffect = 4f;
private AudioLowPassFilter lpFilter;
void Awake() {
lpFilter = Camera.main.GetComponent<AudioLowPassFilter> ();
if (!lpFilter) {
Camera.main.gameObject.AddComponent<AudioLowPassFilter>();
lpFilter = Camera.main.GetComponent<AudioLowPassFilter>();
}
InitLPFilter ();
}
private void InitLPFilter() {
lpFilter.cutoffFrequency = standardLpFreq;
}
}
在协同程序中调整LPF截止频率: }
如果需要更柔和的扫掠,请更新回路内的截止线。您可以使用Mathf.Lerp来计算中间截止频率。恢复正常是什么意思?你是说截止频率应该回到22000吗?是的,没错。。只需将其设置回正常状态,立刻听起来就太奇怪了非常感谢。事实上,我根本不习惯Mathf.Lerp,但它不是只能在0.0和1.0之间进行Lerp吗?
public int standardLpFreq = 22000;
public int hitLpFreq = 280;
public float timeToHaveEffect = 4f;
public float damageBeginTime = 0f;
void HandleHit() {
lpFilter.cutoffFrequency = hitLpFreq;
damageBeginTime = Time.time;
StartCoroutine("DamageSoundEffect");
}
IEnumerator DamageSoundEffect() {
// Wait...
while (Time.time - damageBeginTime < timeToHaveEffect) {
yield return null;
}
// Restore normal sound
lpFilter.cutoffFrequency = standardLpFreq;
}