An example of an audio effects player in Unity that varies the audio's pitch and volume.
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
| // using NaughtyAttributes; // Uncomment if you are using NaughtyAttributes | |
| using UnityEngine; | |
| using Random = UnityEngine.Random; | |
| namespace your.namespace { // Set to your namespace | |
| public class AudioEffectPlayer: MonoBehaviour { | |
| [SerializeField] private float minPitch = 1.0f; | |
| [SerializeField] private float maxPitch = 1.0f; | |
| [SerializeField] private float minVolume = 1.0f; | |
| [SerializeField] private float maxVolume = 1.0f; | |
| [SerializeField] private AudioSource audioSource; | |
| // [Button("Test Play")] // Uncomment if you are using NaughtyAttributes | |
| public void Play() { | |
| this.audioSource.pitch = Random.Range(this.minPitch, this.maxPitch); | |
| this.audioSource.volume = Random.Range(this.minVolume, this.maxVolume); | |
| this.audioSource.Play(); | |
| } | |
| private void OnValidate() { | |
| if (this.audioSource == null) { | |
| this.audioSource = this.gameObject.GetComponent<AudioSource>(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment