FootstepsController.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. using KairoEngine.SFX;
  7. namespace KairoEngine.CharacterSystem
  8. {
  9. public class FootstepsController : SerializedMonoBehaviour
  10. {
  11. public CharacterController character;
  12. public bool emmitWorldSound = true;
  13. //public FootstepsLibrary footstepsLibrary;
  14. public Dictionary<string, SFXClip> footsteps = new Dictionary<string, SFXClip>();
  15. [ReadOnly] public List<string> floorMaterial;
  16. void OnEnable()
  17. {
  18. GenericEvents.StartListening(character.unique_name + "-AnimationStepDone", Footstep);
  19. }
  20. void OnDisable()
  21. {
  22. GenericEvents.StopListening(character.unique_name + "-AnimationStepDone", Footstep);
  23. }
  24. public void OnCollisionEnter(Collision collision)
  25. {
  26. if(collision.gameObject.tag == "Floor")
  27. {
  28. BoxCollider collider = collision.gameObject.GetComponent<BoxCollider>();
  29. if(collider == null) collider = collision.gameObject.GetComponentInChildren<BoxCollider>();
  30. if(collider == null) return;
  31. if(collider.material == null) return;
  32. string material = collider.material.name.Replace(" (Instance)", "");
  33. floorMaterial.Add(material);
  34. //Debug.Log("Added material " + material);
  35. }
  36. }
  37. public void OnCollisionExit(Collision collision)
  38. {
  39. if(collision.gameObject.tag == "Floor")
  40. {
  41. BoxCollider collider = collision.gameObject.GetComponent<BoxCollider>();
  42. if(collider == null) return;
  43. string material = collider.material.name.Replace(" (Instance)", "");
  44. floorMaterial.Remove(material);
  45. //Debug.Log("Removed material " + material);
  46. }
  47. }
  48. private void Footstep()
  49. {
  50. if(floorMaterial.Count < 1) return;
  51. string material = floorMaterial[floorMaterial.Count - 1];
  52. SFXClip sfx;
  53. if (footsteps.TryGetValue(material, out sfx))
  54. {
  55. SoundController.EmmitSound(sfx, character.transform.position);
  56. }
  57. else Debug.Log("No footstep sound found for key " + floorMaterial);
  58. }
  59. }
  60. }