using System.Collections.Generic; using jeanf.validationTools; using UnityEngine; using UnityEngine.Audio; namespace jeanf.universalplayer { /// /// Surface-aware footstep + friction audio, driven directly off /// state (grounded, real velocity, crouch, momentum) instead of the boundary event /// channels — the old project-side footstep wiring silently died whenever the channel /// asset was not hand-wired, and a bool "isMoving" cannot express cadence or friction. /// /// Two layers: /// 1. Footsteps — distance-based stepping (a step every strideLength meters actually /// travelled), so cadence follows walk/sprint/crouch speed for free. The ground /// surface is resolved by raycasting the collider under the feet and matching its /// tag against the surface profiles. /// 2. Scuffs — shoe-on-floor friction when momentum is fought: an abrupt turn or hard /// stop shows up as horizontal acceleration that opposes travel; past a threshold /// the surface's scuff resource plays, louder the faster the player was going. /// Desktop modes only: XRI locomotion snaps velocity, so every VR stick reversal /// would squeak. /// public class FootstepAudio : MonoBehaviour, IValidatable { private const string LogPrefix = "[UniversalPlayer]"; [System.Serializable] public class SurfaceProfile { [Tooltip("Ground collider tag this profile applies to (e.g. 'Concrete').")] public string surfaceTag; [Tooltip("Played each step — an AudioClip or an Audio Random Container.")] public AudioResource footsteps; [Tooltip("Played each step while crouched (sneaking). Optional — falls back to 'footsteps' at crouch volume.")] public AudioResource crouchFootsteps; [Tooltip("Played on abrupt turns/stops (shoe friction). Optional — silent when empty.")] public AudioResource scuffs; [Tooltip("Played on touchdown after a jump/fall (the thud). Optional — falls back to a regular footstep.")] public AudioResource landings; [Range(0f, 2f)] public float volume = 1f; } [Header("Debug")] [Tooltip("Log every step, scuff, surface change and why sounds are skipped.")] [SerializeField] private bool isDebug = false; [Header("Wiring")] [Validation("PlayerMovement is required — footsteps are driven off its grounded/velocity state.")] [SerializeField] private PlayerMovement movement; [Validation("An AudioSource is required — there is nothing to play footsteps through without it.")] [SerializeField] private AudioSource footstepSource; [Tooltip("Optional second source so a scuff can ring over a footstep; falls back to the footstep source.")] [SerializeField] private AudioSource scuffSource; [Header("Surfaces (matched by ground collider tag, first match wins)")] [SerializeField] private List surfaces = new List(); [Tooltip("Used when no profile matches the ground tag — and the packaged default, so assign at least this one.")] [SerializeField] private SurfaceProfile defaultSurface = new SurfaceProfile(); [Header("Ground detection")] [Tooltip("Layers probed for ground. Leave as Nothing for automatic: every layer the player's capsule collides with (the same set it can stand on).")] [SerializeField] private LayerMask groundLayerOverride; [Tooltip("How far below the feet to look for the ground collider.")] [SerializeField] private float rayDistance = 0.5f; [Tooltip("How long a detected surface is trusted before raycasting again.")] [SerializeField] private float surfaceCacheSeconds = 0.2f; [Header("Stepping")] [Tooltip("Meters travelled per footstep. Cadence follows speed automatically (sprint = faster steps).")] [SerializeField] private float strideLength = 0.75f; [Tooltip("Sprinting lengthens the stride a little — real runs take longer strides, not just faster ones.")] [SerializeField] private float sprintStrideMultiplier = 1.25f; [Tooltip("Crouching shortens the stride.")] [SerializeField] private float crouchStrideMultiplier = 0.7f; [Tooltip("Footstep volume multiplier when fully crouched (sneaking is quiet).")] [Range(0f, 1f)] [SerializeField] private float crouchVolume = 0.4f; [Tooltip("Below this speed (m/s) the player is shuffling, not stepping — no footsteps.")] [SerializeField] private float minStepSpeed = 0.2f; [Tooltip("Each step offsets the source this far (m) left/right of center — the L/R foot alternation. Needs a 3D (spatialized) source.")] [SerializeField] private float stepStereoSeparation = 0.25f; [Header("Jump & landing (driven by PlayerEvents from PlayerMovement)")] [Tooltip("Played on jump takeoff (the push-off / effort). Global — not surface-dependent. Optional.")] [SerializeField] private AudioResource jumpSound; [Tooltip("Play a landing sound when the feet hit the ground after a jump or fall (per-surface 'landings', falling back to a regular footstep).")] [SerializeField] private bool stepOnLanding = true; [Tooltip("Minimum downward speed (m/s) on touchdown that counts as a landing (a step off a curb should not thump).")] [SerializeField] private float minLandingFallSpeed = 3f; [Header("Crouch transitions")] [Tooltip("Played when entering the crouch (cloth/gear rustle). Global. Optional.")] [SerializeField] private AudioResource crouchDownSound; [Tooltip("Played when standing back up. Optional — falls back to the crouch-down sound.")] [SerializeField] private AudioResource standUpSound; [Header("Friction scuffs (abrupt turns & hard stops, desktop modes)")] [Tooltip("Horizontal acceleration (m/s²) opposing travel that counts as friction. Compare with PlayerMovement.speedChangeRate (default 8): braking/turning at full rate crosses this, gentle corrections do not.")] [SerializeField] private float scuffAccelThreshold = 6f; [Tooltip("No scuff below this speed (m/s) — sits above a gentle stroll so only committed movement squeaks.")] [SerializeField] private float scuffMinSpeed = 1.6f; [Tooltip("Minimum seconds between scuffs.")] [SerializeField] private float scuffCooldown = 0.35f; [Range(0f, 1f)] [SerializeField] private float scuffVolume = 0.8f; // gates mirrored from PlayerEvents (locomotion freezes must also silence feet) private bool _paused; private bool _menuOpen; private bool _sceneLoading; private float _strideAccumulated; private bool _stepIsLeft; private float _footstepSourceLocalY; private Vector3 _prevCommandedVelocity; private float _lastScuffTime = float.NegativeInfinity; private string _cachedSurfaceTag; private SurfaceProfile _cachedProfile; private float _lastSurfaceCheckTime = float.NegativeInfinity; private bool _warnedNoSounds; /// Diagnostics: the tag of the last surface resolved under the feet (null before the first step). public string CurrentSurfaceTag => _cachedSurfaceTag; /// True when at least one footstep resource is assigned (default or any surface) — without one EVERY step is silent. public bool HasAnyFootstepSound { get { if (defaultSurface != null && defaultSurface.footsteps != null) return true; for (var i = 0; i < surfaces.Count; i++) if (surfaces[i] != null && surfaces[i].footsteps != null) return true; return false; } } /// True when at least one scuff resource is assigned — without one the friction layer (abrupt turns / hard stops) is silent. public bool HasAnyScuffSound { get { if (defaultSurface != null && defaultSurface.scuffs != null) return true; for (var i = 0; i < surfaces.Count; i++) if (surfaces[i] != null && surfaces[i].scuffs != null) return true; return false; } } /// Every non-empty surface profile tag — validation checks each against the project's tag list (an undefined tag can never match a floor). public IEnumerable ConfiguredSurfaceTags { get { for (var i = 0; i < surfaces.Count; i++) if (surfaces[i] != null && !string.IsNullOrEmpty(surfaces[i].surfaceTag)) yield return surfaces[i].surfaceTag; } } /// /// Wired AND audible. The packaged prefab intentionally ships without sounds /// (they are project audio, assigned on the Player variant), so it reports /// invalid until the variant assigns at least one footstep resource — the /// validation banner is the loud version of the runtime one-shot warning. /// public bool IsValid => movement != null && footstepSource != null && HasAnyFootstepSound; private void Awake() { if (footstepSource != null) _footstepSourceLocalY = footstepSource.transform.localPosition.y; } private void OnEnable() { PlayerEvents.PauseRequested += OnPauseRequested; PlayerEvents.MenuStateChanged += OnMenuStateChanged; PlayerEvents.SceneLoadingChanged += OnSceneLoadingChanged; PlayerEvents.PlayerJumped += OnPlayerJumped; PlayerEvents.PlayerLanded += OnPlayerLanded; PlayerEvents.CrouchStateChanged += OnCrouchStateChanged; } private void OnDisable() => Unsubscribe(); private void OnDestroy() => Unsubscribe(); private void Unsubscribe() { PlayerEvents.PauseRequested -= OnPauseRequested; PlayerEvents.MenuStateChanged -= OnMenuStateChanged; PlayerEvents.SceneLoadingChanged -= OnSceneLoadingChanged; PlayerEvents.PlayerJumped -= OnPlayerJumped; PlayerEvents.PlayerLanded -= OnPlayerLanded; PlayerEvents.CrouchStateChanged -= OnCrouchStateChanged; } private void OnPauseRequested(bool paused) => _paused = paused; private void OnMenuStateChanged(bool open) => _menuOpen = open; private void OnSceneLoadingChanged(bool loading) => _sceneLoading = loading; private void Update() { if (movement == null || footstepSource == null) return; var grounded = movement.IsGrounded; var commandedVelocity = movement.PlanarVelocity; if (Frozen) { // Frozen or flying: silence and forget momentum so nothing fires on resume. _strideAccumulated = 0f; _prevCommandedVelocity = commandedVelocity; return; } if (grounded) { DetectScuff(commandedVelocity, Time.deltaTime); var speed = movement.ActualPlanarVelocity.magnitude; if (speed >= minStepSpeed) { _strideAccumulated += speed * Time.deltaTime; if (_strideAccumulated >= CurrentStrideLength()) { _strideAccumulated = 0f; PlayFootstep(); } } else { // Standing (or pushing a wall): never bank a full step for the next start. _strideAccumulated = Mathf.Min(_strideAccumulated, CurrentStrideLength() * 0.5f); } } else { _strideAccumulated = 0f; // feet are off the ground } _prevCommandedVelocity = commandedVelocity; } /// Every state in which feet must be silent: paused, menu open, scene loading, seated, or flying the FreeCam. private bool Frozen => _paused || _menuOpen || _sceneLoading || movement.LocomotionLocked || BroadcastControlsStatus.controlScheme == BroadcastControlsStatus.ControlScheme.Freecam; // ---- locomotion moments (PlayerEvents raised by PlayerMovement) ---- private void OnPlayerJumped() { if (movement == null || footstepSource == null || Frozen) return; if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: jump takeoff.", this); PlayFoley(jumpSound, 1f); } private void OnPlayerLanded(float impactSpeed) { if (movement == null || footstepSource == null || Frozen) return; if (!stepOnLanding || impactSpeed < minLandingFallSpeed) return; _strideAccumulated = 0f; // touchdown IS this stride's step var profile = ResolveSurfaceProfile(); var resource = profile.landings != null ? profile.landings : defaultSurface.landings; if (resource == null) { // No dedicated thud authored: keep the old behavior, a regular footstep. if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: landed at {impactSpeed:F1} m/s — no landing resource, playing a footstep.", this); PlayFootstep(); return; } if (footstepSource.resource != resource) footstepSource.resource = resource; // The harder the fall, the louder the thud (a minimal landing still clearly reads). footstepSource.volume = profile.volume * Mathf.Clamp(impactSpeed / 8f, 0.5f, 1f) * Mathf.Lerp(1f, crouchVolume, movement.CrouchBlend); if (footstepSource.isPlaying) footstepSource.Stop(); footstepSource.Play(); if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: landing on '{_cachedSurfaceTag ?? "default"}' at {impactSpeed:F1} m/s (volume {footstepSource.volume:F2})", this); } private void OnCrouchStateChanged(bool crouched) { if (movement == null || footstepSource == null || Frozen) return; var resource = crouched ? crouchDownSound : (standUpSound != null ? standUpSound : crouchDownSound); if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: {(crouched ? "crouching down" : "standing up")}{(resource == null ? " (no sound assigned)" : "")}.", this); PlayFoley(resource, 0.9f); } /// Non-surface character sounds (jump push-off, crouch rustle) — played on the scuff source so they can ring over a footstep. private void PlayFoley(AudioResource resource, float volume) { if (resource == null) return; var source = scuffSource != null ? scuffSource : footstepSource; if (source.resource != resource) source.resource = resource; source.volume = volume; if (source.isPlaying) source.Stop(); source.Play(); } private float CurrentStrideLength() { var stride = strideLength; if (movement.IsSprinting) stride *= sprintStrideMultiplier; stride *= Mathf.Lerp(1f, crouchStrideMultiplier, movement.CrouchBlend); return Mathf.Max(0.05f, stride); } /// /// Friction = horizontal acceleration that fights the current travel direction /// (braking or turning), measured on the COMMANDED velocity — it ramps at /// PlayerMovement.speedChangeRate, so "player yanked the stick the other way" /// reads as a clean, frame-rate-independent spike. Acceleration ALONG travel /// (speeding up) is excluded: starts never scuff. /// private void DetectScuff(Vector3 commandedVelocity, float dt) { var scheme = BroadcastControlsStatus.controlScheme; if (scheme != BroadcastControlsStatus.ControlScheme.KeyboardMouse && scheme != BroadcastControlsStatus.ControlScheme.Gamepad) return; if (dt <= 0f) return; var referenceSpeed = _prevCommandedVelocity.magnitude; if (referenceSpeed < scuffMinSpeed) return; var acceleration = (commandedVelocity - _prevCommandedVelocity) / dt; var travel = _prevCommandedVelocity / referenceSpeed; var along = Vector3.Dot(acceleration, travel); var friction = (acceleration - Mathf.Max(0f, along) * travel).magnitude; if (friction < scuffAccelThreshold) return; if (Time.time - _lastScuffTime < scuffCooldown) return; _lastScuffTime = Time.time; PlayScuff(referenceSpeed); } private void PlayFootstep() { var profile = ResolveSurfaceProfile(); var resource = profile.footsteps != null ? profile.footsteps : defaultSurface.footsteps; // Sneaking: a dedicated crouch set wins when authored; otherwise the regular // set simply plays at crouch volume (the pre-1.13 behavior). if (movement.IsCrouched) { var crouchResource = profile.crouchFootsteps != null ? profile.crouchFootsteps : defaultSurface.crouchFootsteps; if (crouchResource != null) resource = crouchResource; } if (resource == null) { WarnOnceNoSounds(); return; } if (footstepSource.resource != resource) footstepSource.resource = resource; // Fake L/R feet: alternate the (3D) source around the head center each step. _stepIsLeft = !_stepIsLeft; var local = footstepSource.transform.localPosition; footstepSource.transform.localPosition = new Vector3(_stepIsLeft ? -stepStereoSeparation : stepStereoSeparation, _footstepSourceLocalY, local.z); footstepSource.volume = profile.volume * Mathf.Lerp(1f, crouchVolume, movement.CrouchBlend); if (footstepSource.isPlaying) footstepSource.Stop(); // keep the cadence crisp at sprint speed footstepSource.Play(); if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: step on '{_cachedSurfaceTag ?? "default"}' " + $"({(_stepIsLeft ? "L" : "R")}, volume {footstepSource.volume:F2})", this); } private void PlayScuff(float speed) { var profile = ResolveSurfaceProfile(); var resource = profile.scuffs != null ? profile.scuffs : defaultSurface.scuffs; if (resource == null) { if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: scuff detected on '{_cachedSurfaceTag ?? "default"}' but that surface has no scuff resource.", this); return; } var source = scuffSource != null ? scuffSource : footstepSource; if (source.resource != resource) source.resource = resource; // The faster the player was going, the harder the shoe bites. source.volume = profile.volume * scuffVolume * Mathf.Clamp(speed / 4f, 0.3f, 1f) * Mathf.Lerp(1f, crouchVolume, movement.CrouchBlend); if (source.isPlaying) source.Stop(); source.Play(); if (isDebug) Debug.Log($"{LogPrefix} FootstepAudio: scuff on '{_cachedSurfaceTag ?? "default"}' at {speed:F1} m/s (volume {source.volume:F2})", this); } private SurfaceProfile ResolveSurfaceProfile() { if (Time.time - _lastSurfaceCheckTime < surfaceCacheSeconds && _cachedProfile != null) return _cachedProfile; _lastSurfaceCheckTime = Time.time; var mask = groundLayerOverride.value != 0 ? groundLayerOverride.value : movement.CollidableLayers(); var origin = movement.FootPosition + Vector3.up * 0.1f; if (mask != 0 && Physics.Raycast(origin, Vector3.down, out var hit, rayDistance + 0.1f, mask, QueryTriggerInteraction.Ignore)) { var tag = hit.collider.tag; if (tag != _cachedSurfaceTag && isDebug) Debug.Log($"{LogPrefix} FootstepAudio: surface changed '{_cachedSurfaceTag ?? "none"}' -> '{tag}' ('{hit.collider.name}')", this); _cachedSurfaceTag = tag; _cachedProfile = FindProfile(tag); } else { _cachedSurfaceTag = null; _cachedProfile = defaultSurface; } return _cachedProfile; } private SurfaceProfile FindProfile(string surfaceTag) { for (var i = 0; i < surfaces.Count; i++) { var profile = surfaces[i]; if (profile != null && !string.IsNullOrEmpty(profile.surfaceTag) && profile.surfaceTag == surfaceTag) return profile; } return defaultSurface; } private void WarnOnceNoSounds() { if (_warnedNoSounds) return; _warnedNoSounds = true; Debug.LogWarning($"{LogPrefix} FootstepAudio on '{name}': no footstep AudioResource is assigned " + "(neither the matched surface profile nor the default surface has one) — footsteps are SILENT. " + "Assign AudioClips or Audio Random Containers on your Player variant's FootstepAudio " + "(the packaged prefab intentionally ships without sounds; sample containers live in the AudioSystems package samples).", this); } [System.Diagnostics.Conditional("UNITY_EDITOR")] private void OnDrawGizmosSelected() { if (movement == null) return; Gizmos.color = Color.yellow; var origin = movement.FootPosition + Vector3.up * 0.1f; Gizmos.DrawRay(origin, Vector3.down * (rayDistance + 0.1f)); } } }