Created
February 4, 2022 08:27
-
-
Save scawp/5d8d7c92f3c54ded8dc6dfca768c080c to your computer and use it in GitHub Desktop.
A Basic Audio Modifier for random pitch/volume for an AudioClip #Unity
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class BasicAudioModifier : MonoBehaviour { | |
public List<AudioClip> audioClips = new List<AudioClip>(); | |
[Header("Controls")] | |
public bool mute = false; | |
public bool ignoreAudioSourceClip = false; | |
public bool playOnAwake = false; | |
[Header("Pitch Modifier")] | |
[Tooltip("0.2f to 1f")] | |
[Range(0.2f, 1f)] | |
public float pitchLowRange = .75f; | |
[Tooltip("0.75f to 2f")] | |
[Range(0.75f, 2f)] | |
public float pitchHighRange = 1.25f; | |
[Header("Volume Modifier")] | |
[Tooltip("0.2f to 1f")] | |
[Range(0.2f, 1f)] | |
public float volumeLowRange = .75f; | |
[Tooltip("0.8f to 2f")] | |
[Range(0.8f, 2f)] | |
public float volumeHighRange = 1f; | |
private AudioSource audioSource; | |
void Awake () { | |
audioSource = GetComponent<AudioSource>(); | |
if(!ignoreAudioSourceClip) { | |
audioClips.Add(audioSource.clip); | |
} | |
//delete any null clips, add an if statement if desirable? | |
audioClips.RemoveAll(clip => clip.Equals(null)); | |
//TODO: when audioSource.playOnAwake is true the event fires before I can override it | |
if(audioSource.playOnAwake) { | |
Debug.Log("AudioSource.playOnAwake is true, should be false"); | |
} | |
if(playOnAwake) { | |
Play(); | |
} | |
} | |
void Start () { | |
} | |
public void Play () { | |
if (!mute) { | |
audioSource.pitch = Random.Range(pitchLowRange, pitchHighRange); | |
audioSource.volume = Random.Range(volumeLowRange, volumeHighRange); | |
int clipId = Random.Range(0, audioClips.Count); | |
Debug.Log("playing clip: " + audioClips[clipId].name); | |
audioSource.clip = audioClips[clipId]; | |
audioSource.Play(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BasicAudioModifier
A simple script to change the pitch/volume of an AudioClip.
Usage
Drop the script onto your
GameObject
containing theAudioSource
. Modify the options as desired.Set
PlayOnAwake
tofalse
inAudioSource
, usePlayOnAwake
in the script so the modifiers take effect.call
GetComponent<BasicAudioModifier>().Play();
to replay clip (clip will have a new pitch/volume).Multiple
AudioClip
s can be added and one will be chosen at random.if
ignoreAudioSourceClip
istrue
then the existingAudioClip
inAudioSource
will be ignored else it will be added to the random list.Don't like something here? Fork and modify needed! Let me know if you come up with something cool ;)
Why?
Hearing the same noise over and over was driving me insane, this helps. For bigger projects I'd recommend you use something like
Fmod
orblenda
but this is fine for simple needs.