Skip to content

Instantly share code, notes, and snippets.

@RamenSea
Created April 26, 2023 00:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
An example of an audio effects player in Unity that varies the audio's pitch and volume.
// 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