Class Brakes
Vehicle braking system that manages brake torque distribution to wheels, handbrake operation, and various automatic braking behaviors like off-throttle braking and idle braking.
Inherited Members
Namespace: NWH.VehiclePhysics2
Assembly: NWH.VehiclePhysics2.dll
Syntax
[Serializable]
public class Brakes : VehicleComponent
Remarks
The Brakes component handles all aspects of vehicle braking including manual brake input, handbrake operation, automatic braking scenarios, and brake torque modification through external systems like ABS. The actual brake force application occurs within each WheelController.
Key features include: - Manual brake input with configurable actuation time for realistic pedal response - Handbrake support with both standard (hold) and latching (toggle) modes - Off-throttle braking simulation for engine compression and electric regenerative braking - Reverse direction braking to prevent backing into obstacles when input conflicts with movement - Idle braking to prevent rolling when parked in neutral - Brake torque modifier system for ABS, stability control, and other assistance systems
The brake torque is distributed to wheels through their respective WheelGroups, allowing for different braking characteristics on front/rear axles through brake coefficients. Maximum brake torque should be tuned based on vehicle weight and desired braking performance.
Fields
actuationTime
Time in seconds for brakes to reach full actuation from zero input. Controls brake pedal response smoothing to simulate hydraulic brake system lag.
Declaration
[Range(0, 1)]
[Tooltip("Higher smoothing will result in brakes being applied more gradually.")]
public float actuationTime
Field Value
| Type | Description |
|---|---|
| float |
Remarks
This value simulates the time delay between pressing the brake pedal and achieving full braking force, mimicking real brake system hydraulic response and brake pad engagement.
Low values (0.05-0.1s): Quick, responsive braking. Good for racing and arcade games. Medium values (0.1-0.2s): Realistic car-like response. Good for simulation games. High values (0.3-0.5s): Sluggish braking. Good for heavy vehicles or worn brake systems.
brakeOffThrottleIntensity
Braking force applied when throttle is released, as a percentage of maximum brake torque. Simulates engine compression braking or electric vehicle regenerative braking.
Declaration
[Range(0, 1)]
[Tooltip(" Strength of off-throttle braking in percentage [0 to 1] of max braking torque.\r\n Set to 0 to disable braking off-throttle.")]
public float brakeOffThrottleIntensity
Field Value
| Type | Description |
|---|---|
| float |
Remarks
This feature simulates the natural deceleration that occurs when lifting off the throttle in real vehicles due to engine compression, transmission drag, or electric motor regeneration.
0.0: No off-throttle braking - vehicle coasts freely when throttle is released. 0.1-0.3: Light braking - simulates gasoline engine compression braking or mild regeneration. 0.4-0.7: Medium braking - simulates diesel engine compression or strong regenerative braking. 0.8-1.0: Strong braking - simulates engine braking in low gears or maximum regeneration mode.
Useful for electric vehicles, manual transmissions, or any scenario where releasing throttle should provide noticeable deceleration rather than pure coasting.
brakeOnReverseDirection
Enables automatic braking when the vehicle is moving in the opposite direction to the throttle input. Prevents unintended acceleration when trying to stop or change direction.
Declaration
[Tooltip("Should the vehicle apply brakes when the movement direction is opposite of the input direction?")]
public bool brakeOnReverseDirection
Field Value
| Type | Description |
|---|---|
| bool |
Remarks
This safety feature applies brakes when there's a conflict between intended direction (throttle input) and actual movement direction. For example, if the vehicle is rolling backwards but the player presses forward throttle, brakes are applied to stop the backwards motion before acceleration begins.
Enable for: Realistic vehicle behavior, safety in racing games, preventing rollback on hills. Disable for: Pure arcade behavior, when implementing custom direction change logic, or when conflicting behavior is desired for gameplay reasons.
brakeTorqueModifiers
List of delegate functions that modify brake torque before application to wheels. Used by modules like ABS, stability control, and traction management systems.
Declaration
[Tooltip("Collection of functions that modify the braking performance of the vehicle. Used for modules such as ABS where brakes need to be overriden or their effect reduced/increase. Return 1 for neutral modifier while returning 0 will disable the brakes completely. All brake torque modifiers will be multiplied in order to get the final brake torque coefficient.")]
public List<Brakes.BrakeTorqueModifier> brakeTorqueModifiers
Field Value
| Type | Description |
|---|---|
| List<Brakes.BrakeTorqueModifier> |
Remarks
Each modifier function should return a multiplier value (0.0 to 1.0+) that will be applied to the final brake torque. All modifiers are multiplied together to get the final coefficient.
Return values: - 1.0: No modification (neutral) - 0.5: Reduce braking to 50% (partial ABS intervention) - 0.0: Disable braking completely (full ABS intervention) - 1.5: Increase braking to 150% (brake assist systems)
Modules automatically register their modifier functions when enabled and remove them when disabled. Multiple brake assistance systems can work together via this list.
brakeWhileDisabled
Should brakes be applied when the vehicle is disabled?
Declaration
[FormerlySerializedAs("brakeWhileAsleep")]
[ShowInSettings("Brake While Disabled")]
[Tooltip("Should brakes be applied when the vehicle is disabled?")]
public bool brakeWhileDisabled
Field Value
| Type | Description |
|---|---|
| bool |
brakeWhileIdle
If true, the vehicle will brake when in neutral and no throttle is applied.
Declaration
[ShowInSettings("Brake While Idle")]
[Tooltip("If true, the vehicle will brake when in neutral and no throttle is applied.")]
public bool brakeWhileIdle
Field Value
| Type | Description |
|---|---|
| bool |
damage
Current damage level of the braking system (0-1). At full damage, brakes lose effectiveness.
Declaration
[NonSerialized]
[Tooltip("Current brake system damage (0-1).")]
public float damage
Field Value
| Type | Description |
|---|---|
| float |
damageBrakeTorqueReduction
Maximum brake torque reduction at full damage (0-1). 0.6 = 60% brake torque loss when damage=1.
Declaration
[Range(0, 0.8)]
[Tooltip("Maximum brake torque reduction at full damage (0-1). 0.6 = 60% loss at damage=1.")]
public float damageBrakeTorqueReduction
Field Value
| Type | Description |
|---|---|
| float |
handbrakeType
How the handbrake responds to input. Standard holds while pressed, Latching toggles on/off.
Declaration
[ShowInSettings("Handbrake Type")]
[Tooltip("How the handbrake responds to input. Standard holds while pressed, Latching toggles on/off.")]
public Brakes.HandbrakeType handbrakeType
Field Value
| Type | Description |
|---|---|
| Brakes.HandbrakeType |
handbrakeValue
Current value of the handbrake. 0 = inactive, 1 = maximum strength. Handbrake strength will also be affected by per wheel group handbrake settings.
Declaration
[Tooltip(" Current value of the handbrake. 0 = inactive, 1 = maximum strength.\r\n Handbrake strength will also be affected by per wheel group handbrake settings.")]
public float handbrakeValue
Field Value
| Type | Description |
|---|---|
| float |
maxTorque
Max brake torque that can be applied to each wheel. To adjust braking on a per-axle basis, change brake coefficients under Wheel Group settings.
Declaration
[Tooltip("Max brake torque that can be applied to each wheel. To adjust braking on per-axle basis change brake coefficients under Wheel Group settings.")]
[ShowInSettings("Max. Torque")]
public float maxTorque
Field Value
| Type | Description |
|---|---|
| float |
onBrakesActivate
Called each time brakes are activated.
Declaration
[Tooltip("Called each time brakes are activated.")]
public UnityEvent onBrakesActivate
Field Value
| Type | Description |
|---|---|
| UnityEvent |
onBrakesDeactivate
Called each time brakes are released.
Declaration
[Tooltip("Called each time brakes are released.")]
public UnityEvent onBrakesDeactivate
Field Value
| Type | Description |
|---|---|
| UnityEvent |
Properties
IsBraking
Returns true if the vehicle is currently braking. Will return true if there is ANY brake torque applied to the wheels.
Declaration
public bool IsBraking { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
AddBrakeTorqueAllWheels(float, bool)
Applies brake torque to every wheel, clamped to maxTorque and scaled by current damage.
Declaration
public void AddBrakeTorqueAllWheels(float brakeTorque, bool isHandbrake = false)
Parameters
| Type | Name | Description |
|---|---|---|
| float | brakeTorque | Brake torque in Nm. Clamped to [0, maxTorque]. |
| bool | isHandbrake | True if the torque comes from the handbrake. |
AddExternalBrakeTorque(int, float)
Accumulates per-wheel brake torque (Nm) from an external system such as ESC. Applied after the per-frame reset in VC_FixedUpdate, so it survives regardless of caller order.
Declaration
public void AddExternalBrakeTorque(int wheelIndex, float brakeTorque)
Parameters
| Type | Name | Description |
|---|---|---|
| int | wheelIndex | Index into powertrain.wheels. |
| float | brakeTorque | Brake torque in Nm. Negative values are ignored. |
VC_Disable(bool)
Disables the component and stops updates. Override to add component-specific disable logic.
Declaration
public override bool VC_Disable(bool calledByParent)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | calledByParent | True if called by parent component or LOD system |
Returns
| Type | Description |
|---|---|
| bool | True if successfully disabled, false otherwise |
Overrides
Remarks
Component must be initialized before it can be disabled. Tracks whether disable was called by parent to manage LOD system interaction.
VC_FixedUpdate(float)
Physics update called at fixed intervals. Override to implement physics-based behavior.
Declaration
public override void VC_FixedUpdate(float DeltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | DeltaTime | Time elapsed since last FixedUpdate in seconds |
Overrides
Remarks
Only called when the component is active (initialized and enabled). Use for physics calculations, force application, and other fixed-timestep operations. Called from VehicleController.FixedUpdate().
VC_Initialize()
Initializes the component's internal systems and resources. Called once during vehicle startup after the VehicleController reference is set.
Declaration
protected override void VC_Initialize()
Overrides
Remarks
Override this to set up component-specific resources, cache references, or perform one-time initialization. Sets state.initialized to true on completion.
VC_Validate(VehicleController)
Validates the component configuration and reports any setup issues. Override to implement component-specific validation rules.
Declaration
public override void VC_Validate(VehicleController vc)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vc | Parent VehicleController to validate against |
Overrides
Remarks
Called in Editor when validation is triggered manually or automatically. Should use Debug.LogWarning/Error to report configuration problems. Check for null references, invalid values, missing dependencies, etc.