You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

65 lines
1.4 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
using Random = UnityEngine.Random;
public class AudioRandomiser : MonoBehaviour
{
public AudioClip[] audioSources;
public HerdController herd;
public GameObject audioPosition;
public float waitTime;
public float[] volumes;
// Start is called before the first frame update
void Start()
{
}
void Awake()
{
StartCoroutine(PlayClips());
}
public void PlaySoundFromCount()
{
int count = 1;
if (herd != null)
{
count = (int) Mathf.Round(herd.HerdAmount() / 10);
count = Mathf.Clamp(count, 1, 10);
}
for (int i = 0; i < count; i++)
{
StartCoroutine(RandomSound());
}
}
public IEnumerator RandomSound()
{
float wait = Random.Range(0.5f, 4.5f);
yield return new WaitForSeconds(wait);
AudioClip clip = audioSources[Random.Range(0, audioSources.Length)];
float volume = Random.Range(volumes[0], volumes[1]);
AudioSource.PlayClipAtPoint(clip, audioPosition.transform.position, volume);
}
public IEnumerator PlayClips()
{
yield return new WaitForSeconds(waitTime);
PlaySoundFromCount();
StartCoroutine(PlayClips());
}
// Update is called once per frame
void Update()
{
}
}