I’m making a pitch randomizer for my weapon audio sources in my game, and I want to be able to attach a script and just have the pitch change every time the audio source plays. I tried using UnityEvents to get this effect, but couldn’t quite get it to work in the way I wanted. If I could get some advice pointing me in the right direction it would be much appreciated ![]()
using UnityEngine;
using UnityEngine.Events;
public class AudioPitchRandomizer : MonoBehaviour
{
[SerializeField] float range;
private AudioSource source;
UnityEvent OnAudioPlayed = new UnityEvent();
private void Start()
{
source = GetComponent<AudioSource>();
OnAudioPlayed.AddListener(source.Play);
}
private void Update()
{
if(OnAudioPlayed)
{
ChangePitch();
}
}
void ChangePitch()
{
source.pitch = 1 + Random.Range(-range, range);
}
}