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

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using JetBrains.Annotations;
  5. using UnityEngine;
  6. using Random = UnityEngine.Random;
  7. public class AudioRandomiser : MonoBehaviour
  8. {
  9. public AudioClip[] audioSources;
  10. public HerdController herd;
  11. public GameObject audioPosition;
  12. public float waitTime;
  13. public float[] volumes;
  14. // Start is called before the first frame update
  15. void Start()
  16. {
  17. }
  18. void Awake()
  19. {
  20. StartCoroutine(PlayClips());
  21. }
  22. public void PlaySoundFromCount()
  23. {
  24. int count = 1;
  25. if (herd != null)
  26. {
  27. count = (int) Mathf.Round(herd.HerdAmount() / 10);
  28. count = Mathf.Clamp(count, 1, 10);
  29. }
  30. for (int i = 0; i < count; i++)
  31. {
  32. StartCoroutine(RandomSound());
  33. }
  34. }
  35. public IEnumerator RandomSound()
  36. {
  37. float wait = Random.Range(0.5f, 4.5f);
  38. yield return new WaitForSeconds(wait);
  39. AudioClip clip = audioSources[Random.Range(0, audioSources.Length)];
  40. float volume = Random.Range(volumes[0], volumes[1]);
  41. AudioSource.PlayClipAtPoint(clip, audioPosition.transform.position, volume);
  42. }
  43. public IEnumerator PlayClips()
  44. {
  45. yield return new WaitForSeconds(waitTime);
  46. PlaySoundFromCount();
  47. StartCoroutine(PlayClips());
  48. }
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. }
  53. }