Component attached to a player that plays a landing impact sound when the player falls farther than a threshold. It listens to PlayerController landing events, checks local ownership, and broadcasts a RPC to play the sound on clients.
using Sandbox;
public sealed class PlayerImpactSound : Component, PlayerController.IEvents
{
[Property] public PlayerController PlayerController { get; set; }
[Property] public SoundEvent Sound { get; set; }
// Fallback for scenes with no TileManager (e.g. lobby).
private readonly float DefaultMinFallDistance = TileManager.Current?.LayerSpacing * 1.2f ?? 400f;
[Rpc.Broadcast]
private void BroadcastSound()
{
GameObject.PlaySound( Sound );
}
void PlayerController.IEvents.OnLanded( float distance, Vector3 impactVelocity )
{
if ( GameObject.Network.Owner != Connection.Local ) return;
if ( distance > DefaultMinFallDistance ) BroadcastSound();
}
}