Last active
August 27, 2021 20:53
-
-
Save JoePotchen/200e4d4beb87cf140de71999f2dedc82 to your computer and use it in GitHub Desktop.
Udon Audio Low Pass Trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Meant to be thrown on a collider that triggers the effect. Could definitely be made better, rushed it for a project. | |
using UdonSharp; | |
using UnityEngine; | |
using VRC.SDKBase; | |
using VRC.Udon; | |
public class AudioLowPassTrigger : UdonSharpBehaviour | |
{ | |
public AudioSource AudioSource; | |
public AudioLowPassFilter LowPassFilter; | |
public AudioReverbFilter ReverbFilter; | |
public float CutOffFrequency = 140; | |
private float volume; | |
private int state; | |
public void Start() | |
{ | |
volume = AudioSource.volume; | |
LowPassFilter.cutoffFrequency = CutOffFrequency; | |
ReverbFilter.enabled = true; | |
} | |
public override void OnPlayerTriggerEnter(VRCPlayerApi player) | |
{ | |
if (player.isLocal) | |
{ | |
state = 1; | |
} | |
} | |
public override void OnPlayerTriggerExit(VRCPlayerApi player) | |
{ | |
if (player.isLocal) | |
{ | |
volume = AudioSource.volume; | |
state = 2; | |
} | |
} | |
public void Update() | |
{ | |
if(state == 0) | |
{ | |
} | |
else if (state == 1) //Entering | |
{ | |
LowPassFilter.cutoffFrequency = Mathf.Min(LowPassFilter.cutoffFrequency + 500, 22000); | |
AudioSource.volume = Mathf.Min(AudioSource.volume + .1f, volume); | |
if (LowPassFilter.cutoffFrequency >= 22000) | |
{ | |
ReverbFilter.enabled = false; | |
state = 0; | |
} | |
} | |
else if (state == 2) //Exiting | |
{ | |
ReverbFilter.enabled = true; | |
LowPassFilter.cutoffFrequency = Mathf.Max(LowPassFilter.cutoffFrequency - 500, CutOffFrequency); | |
AudioSource.volume = Mathf.Max(AudioSource.volume - .1f, .110f); | |
if (LowPassFilter.cutoffFrequency <= CutOffFrequency) | |
{ | |
AudioSource.volume = .110f; | |
state = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment