feat : basic footstep controller system
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using AK.Wwise;
|
||||
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class Audio_FootstepsController : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct SurfaceMapping
|
||||
{
|
||||
public string tag;
|
||||
public AK.Wwise.Switch wwiseSwitch;
|
||||
}
|
||||
|
||||
[Header("Wwise Assets")]
|
||||
public AK.Wwise.Event footstepEvent;
|
||||
|
||||
[Header("Surface Configuration")]
|
||||
public List<SurfaceMapping> surfaces = new List<SurfaceMapping>();
|
||||
public AK.Wwise.Switch defaultSwitch;
|
||||
|
||||
[Header("Movement Settings")]
|
||||
[SerializeField] private float stepInterval = 0.5f;
|
||||
[SerializeField] private float minVelocity = 0.1f;
|
||||
[SerializeField] private float rayDistance = 1.2f;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
|
||||
private CharacterController _controller;
|
||||
private float _timer;
|
||||
private Dictionary<string, AK.Wwise.Switch> _surfaceCache;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
|
||||
// On remplit le dictionnaire pour un accès ultra-rapide par le nom du tag
|
||||
_surfaceCache = new Dictionary<string, AK.Wwise.Switch>();
|
||||
foreach (var mapping in surfaces)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(mapping.tag))
|
||||
{
|
||||
// On stocke en minuscule pour éviter les erreurs de casse
|
||||
_surfaceCache[mapping.tag.ToLower()] = mapping.wwiseSwitch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_controller.isGrounded && _controller.velocity.magnitude > minVelocity)
|
||||
{
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer >= stepInterval)
|
||||
{
|
||||
PlayFootstep();
|
||||
_timer = 0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_timer = stepInterval;
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayFootstep()
|
||||
{
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(transform.position + Vector3.up * 0.1f, Vector3.down, out hit, rayDistance, groundLayer))
|
||||
{
|
||||
string currentTag = hit.collider.tag.ToLower();
|
||||
|
||||
// On cherche le switch dans le dictionnaire
|
||||
if (_surfaceCache.TryGetValue(currentTag, out AK.Wwise.Switch targetSwitch))
|
||||
{
|
||||
targetSwitch.SetValue(gameObject);
|
||||
// Debug.Log($"[Footsteps] Switch Wwise réglé sur : {currentTag}");
|
||||
}
|
||||
else
|
||||
{
|
||||
defaultSwitch.SetValue(gameObject);
|
||||
Debug.LogWarning($"[Footsteps] Tag '{currentTag}' inconnu. Utilisation du switch par défaut.");
|
||||
}
|
||||
|
||||
footstepEvent.Post(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a11b07babb12d446a356745e7a37303
|
||||
Reference in New Issue
Block a user