Code/Sound/WheelSkid.cs
using System;
using Meteor.VehicleTool.Vehicle;
using Sandbox;

namespace Meteor.VehicleTool;

public class WheelSkid : Component
{
	[Property] VehicleController controller { get; set; }

	[Property] SoundFile SkidSound { get; set; }
	[Property, Range( 0, 2 )] float Volume { get; set; } = 1;

	private SoundHandle SoundHandle;
	protected override void OnStart()
	{
		SoundHandle = Sound.PlayFile( SkidSound );
	}
	protected override void OnUpdate()
	{
		float newVolume = 0f;

		foreach ( var item in controller.Wheels )
		{
			if ( !item.IsGrounded )
				continue;
			bool isSkidding = item.IsSkiddingLaterally || item.IsSkiddingLongitudinally;
			if ( !isSkidding ) continue;

			float slipPercent = Math.Clamp( item.NormalizedLateralSlip + item.NormalizedLongitudinalSlip, 0, 1 );
			float speedCoeff = MathF.Min( 1.2f, controller.CurrentSpeed * 0.03f + item.AngularVelocity * 0.05f );
			float wheelVolume = slipPercent * speedCoeff;
			newVolume = newVolume > wheelVolume ? newVolume : wheelVolume;

		}

		SoundHandle.Position = WorldPosition;
		SoundHandle.Volume = newVolume * Volume;
		SoundHandle.Pitch = Math.Clamp( newVolume, 0.9f, 1.2f );
	}
}