FootstepEvents.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Samples.Events
  5. {
  6. /// <summary>Uses Animancer Events to play a sound randomly selected from an array.</summary>
  7. ///
  8. /// <remarks>
  9. /// <strong>Sample:</strong>
  10. /// <see href="https://kybernetik.com.au/animancer/docs/samples/events/footsteps">
  11. /// Footstep Events</see>
  12. /// </remarks>
  13. ///
  14. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Events/FootstepEvents
  15. ///
  16. [AddComponentMenu(Strings.SamplesMenuPrefix + "Footstep Events - Footstep Events")]
  17. [AnimancerHelpUrl(typeof(FootstepEvents))]
  18. public class FootstepEvents : MonoBehaviour
  19. {
  20. /************************************************************************************************************************/
  21. #if UNITY_AUDIO
  22. /************************************************************************************************************************/
  23. [SerializeField] private AnimancerComponent _Animancer;
  24. [SerializeField] private StringAsset _EventName;
  25. [SerializeField, Range(0, 1)] private float _PitchRandomization = 0.2f;
  26. [SerializeField] private AudioClip[] _Sounds;
  27. /************************************************************************************************************************/
  28. protected virtual void Awake()
  29. {
  30. _Animancer.Events.AddTo<AudioSource>(_EventName, PlaySound);
  31. }
  32. /************************************************************************************************************************/
  33. private void PlaySound(AudioSource source)
  34. {
  35. source.clip = _Sounds[Random.Range(0, _Sounds.Length)];
  36. source.pitch = Random.Range(1 - _PitchRandomization, 1 + _PitchRandomization);
  37. source.volume = AnimancerEvent.Current.State.Weight;
  38. source.Play();
  39. // Create a sphere on the foot to show where the sound is coming from.
  40. GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  41. Transform transform = sphere.transform;
  42. transform.parent = source.transform;
  43. transform.localPosition = Vector3.zero;
  44. transform.localScale = Vector3.one * 0.2f;
  45. Destroy(sphere, 0.1f);// Destroy after 0.1 seconds.
  46. }
  47. /************************************************************************************************************************/
  48. #else
  49. /************************************************************************************************************************/
  50. protected virtual void Awake()
  51. {
  52. SampleReadMe.LogMissingAudioModuleError(this);
  53. }
  54. /************************************************************************************************************************/
  55. #endif
  56. /************************************************************************************************************************/
  57. }
  58. }