using UnityEngine; namespace KhawajjaGames.Audio { /// /// Lightweight, zero-garbage-collection audio SFX playback helper. /// Provides pitch randomization and spatial audio handling for mobile Unity games. /// License: Khawajja Games Asset License (Free for commercial & personal projects). /// public static class AudioSFXHelper { /// /// Plays a sound clip at a world position with subtle pitch variation. /// public static AudioSource PlayClipAtPointWithPitch(AudioClip clip, Vector3 position, float volume = 1f, float minPitch = 0.9f, float maxPitch = 1.1f) { if (clip == null) return null; GameObject tempGO = new GameObject("TempAudio_" + clip.name); tempGO.transform.position = position; AudioSource audioSource = tempGO.AddComponent(); audioSource.clip = clip; audioSource.volume = Mathf.Clamp01(volume); audioSource.pitch = Random.Range(minPitch, maxPitch); audioSource.spatialBlend = 1f; // 3D Audio audioSource.minDistance = 5f; audioSource.maxDistance = 150f; audioSource.rolloffMode = AudioRolloffMode.Linear; audioSource.Play(); Object.Destroy(tempGO, clip.length / Mathf.Max(0.1f, audioSource.pitch) + 0.1f); return audioSource; } /// /// Plays a 2D interface or HUD sound effect on an existing AudioSource. /// public static void Play2DOneShot(AudioSource source, AudioClip clip, float volume = 1f, float minPitch = 0.95f, float maxPitch = 1.05f) { if (source == null || clip == null) return; source.pitch = Random.Range(minPitch, maxPitch); source.PlayOneShot(clip, volume); } } }