using UdonSharp; using UnityEngine; [UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)] [DisallowMultipleComponent] [AddComponentMenu("Artyom Scripting System/Impact Sound Player")] public class ImpactSoundPlayer : UdonSharpBehaviour { public GameObject smallImpactSoundObject; // GameObject for small impact sound public GameObject mediumImpactSoundObject; // GameObject for medium impact sound public GameObject largeImpactSoundObject; // GameObject for large impact sound public float mediumImpactThreshold = 5.0f; // Threshold for medium impact public float largeImpactThreshold = 10.0f; // Threshold for large impact [Tooltip("Impacts slower than this play nothing. 0 plays a sound for every contact, however gentle.")] public float minimumImpact = 0f; [Tooltip("Seconds after a sound before the next can play. 0 means no limit.")] public float cooldown = 0f; private float nextSoundTime; private void OnCollisionEnter(Collision collision) { float impactForce = collision.relativeVelocity.magnitude; if (impactForce < minimumImpact || Time.time < nextSoundTime) return; nextSoundTime = Time.time + cooldown; if (impactForce >= largeImpactThreshold) { PlaySound(largeImpactSoundObject); } else if (impactForce >= mediumImpactThreshold) { PlaySound(mediumImpactSoundObject); } else { PlaySound(smallImpactSoundObject); } } private void PlaySound(GameObject soundObject) { if (soundObject != null) { soundObject.SetActive(false); // Reset the sound object soundObject.SetActive(true); // Activate the sound object to play the sound } } }