1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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<string, SFXClip> footsteps = new Dictionary<string, SFXClip>();
-
- [ReadOnly] public List<string> 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<BoxCollider>();
- if(collider == null) collider = collision.gameObject.GetComponentInChildren<BoxCollider>();
- 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<BoxCollider>();
- 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);
- }
- }
- }
|