Component attached to a player that plays footstep/collision sounds when the player is near the ground while walking or running. It traces a box downward each update, checks surface hit and player movement state, and broadcasts a RPC to play the collision sound with different cooldowns for run vs walk.
using Sandbox;
using System;
public class Player__SFX : Component {
[Property] public Player__Controller Player_Controller {get;set;}
TimeUntil Next;
protected override void OnUpdate() {
if (IsProxy) return;
var From = WorldPosition;
var To = From + new Vector3(0, 0, -64);
SceneTraceResult Box = Scene.Trace
.Box(BBox.FromPositionAndSize(0, 64), From, To)
.IgnoreGameObject(GameObject).Run();
if (Box.Hit && (Player_Controller.State == "run" || Player_Controller.State == "walk") && Next) {
PlayCollisionSound(Box.Surface, WorldPosition, 256f);
if (Player_Controller.State == "run") {
Next = .25f;
return;
}
Next = .4f;
}
}
[Rpc.Broadcast]
public void PlayCollisionSound(Surface surface, Vector3 vector3, float Float) {
surface.PlayCollisionSound(vector3, Float);
}
}