FlockChildSound.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. [RequireComponent(typeof(AudioSource))]
  3. public class FlockChildSound:MonoBehaviour{
  4. public AudioClip[] _idleSounds;
  5. public float _idleSoundRandomChance = .05f;
  6. public AudioClip[] _flightSounds;
  7. public float _flightSoundRandomChance = .05f;
  8. public AudioClip[] _scareSounds;
  9. public float _pitchMin = .85f;
  10. public float _pitchMax = 1.0f;
  11. public float _volumeMin = .6f;
  12. public float _volumeMax = .8f;
  13. FlockChild _flockChild;
  14. AudioSource _audio;
  15. bool _hasLanded;
  16. public void Start() {
  17. _flockChild = GetComponent<FlockChild>();
  18. _audio = GetComponent<AudioSource>();
  19. InvokeRepeating("PlayRandomSound", Random.value+1, 1.0f);
  20. if(_scareSounds.Length > 0)
  21. InvokeRepeating("ScareSound", 1.0f, .01f);
  22. }
  23. public void PlayRandomSound() {
  24. if(gameObject.activeInHierarchy){
  25. if(!_audio.isPlaying && _flightSounds.Length > 0 && _flightSoundRandomChance > Random.value && !_flockChild._landing){
  26. _audio.clip = _flightSounds[Random.Range(0,_flightSounds.Length)];
  27. _audio.pitch = Random.Range(_pitchMin, _pitchMax);
  28. _audio.volume = Random.Range(_volumeMin, _volumeMax);
  29. _audio.Play();
  30. }else if(!_audio.isPlaying && _idleSounds.Length > 0 && _idleSoundRandomChance > Random.value && _flockChild._landing){
  31. _audio.clip = _idleSounds[Random.Range(0,_idleSounds.Length)];
  32. _audio.pitch = Random.Range(_pitchMin, _pitchMax);
  33. _audio.volume = Random.Range(_volumeMin, _volumeMax);
  34. _audio.Play();
  35. _hasLanded = true;
  36. }
  37. }
  38. }
  39. public void ScareSound() {
  40. if(gameObject.activeInHierarchy){
  41. if(_hasLanded && !_flockChild._landing && _idleSoundRandomChance*2 > Random.value){
  42. _audio.clip = _scareSounds[Random.Range(0,_scareSounds.Length)];
  43. _audio.volume = Random.Range(_volumeMin, _volumeMax);
  44. _audio.PlayDelayed(Random.value*.2f);
  45. _hasLanded = false;
  46. }
  47. }
  48. }
  49. }