Hi Guys, how can I avoid repetitions with Random.Range? Is it also possible to have it do a shuffle mode like Wwise where no repetitions happens until all the audio clips in the array have played at least once?
Thanks!
using UnityEngine;
using System.Collections;
public class JKPlayStingerOnPlayerEntry : MonoBehaviour {
public AudioSource stinger;
public AudioClip[] audioClips;
public float lowPitchRange = .95f;
public float highPitchRange = 1.05f;
public bool playOnlyOnce;
private bool _hasPlayedOnce;
void OnTriggerEnter(Collider other)
{
if(playOnlyOnce && _hasPlayedOnce) return;
if(!other.CompareTag ("Player")) return;
float randomPitch = Random.Range(lowPitchRange, highPitchRange);
stinger.pitch = randomPitch;
int randomIndex = Random.Range(0, audioClips.Length);
stinger.clip = audioClips[randomIndex];
stinger.Play ();
_hasPlayedOnce = true;
}
}
Have one array listing clips, another for the randomised copy. Shuffle the source array into the second array, use the last one and reduce a counter until it reaches zero. Shuffle again.