using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; using KairoEngine.SFX; namespace KairoEngine.CharacterSystem { public class FootstepsController : SerializedMonoBehaviour { public CharacterController character; public bool emmitWorldSound = true; //public FootstepsLibrary footstepsLibrary; public Dictionary footsteps = new Dictionary(); [ReadOnly] public List floorMaterial; void OnEnable() { GenericEvents.StartListening(character.unique_name + "-AnimationStepDone", Footstep); } void OnDisable() { GenericEvents.StopListening(character.unique_name + "-AnimationStepDone", Footstep); } public void OnCollisionEnter(Collision collision) { if(collision.gameObject.tag == "Floor") { BoxCollider collider = collision.gameObject.GetComponent(); if(collider == null) collider = collision.gameObject.GetComponentInChildren(); if(collider == null) return; if(collider.material == null) return; string material = collider.material.name.Replace(" (Instance)", ""); floorMaterial.Add(material); //Debug.Log("Added material " + material); } } public void OnCollisionExit(Collision collision) { if(collision.gameObject.tag == "Floor") { BoxCollider collider = collision.gameObject.GetComponent(); if(collider == null) return; string material = collider.material.name.Replace(" (Instance)", ""); floorMaterial.Remove(material); //Debug.Log("Removed material " + material); } } private void Footstep() { if(floorMaterial.Count < 1) return; string material = floorMaterial[floorMaterial.Count - 1]; SFXClip sfx; if (footsteps.TryGetValue(material, out sfx)) { SoundController.EmmitSound(sfx, character.transform.position); } else Debug.Log("No footstep sound found for key " + floorMaterial); } } }