Class TrailerModule
Module for vehicles that can be towed as trailers.
Inherited Members
Namespace: NWH.VehiclePhysics2.Modules.Trailer
Assembly: NWH.VehiclePhysics2.dll
Syntax
[Serializable]
public class TrailerModule : VehicleComponent
Remarks
The TrailerModule allows a vehicle to be attached to and towed by vehicles equipped with TrailerHitchModule. It handles the trailer-side connection logic, input synchronization, and state management when attached or detached.
Key features: - Automatic input state synchronization from towing vehicle - Optional gear synchronization for powered trailers - Trailer stand management for stability when detached - Attachment point configuration for proper connection alignment - Events for attachment and detachment notifications
The module creates a trigger collider at the attachment point that the TrailerHitchModule detects for connection. When attached, the trailer receives input states from the towing vehicle, allowing synchronized braking, lighting, and optional powered operation.
Both TrailerModule and TrailerHitchModule can exist on the same vehicle for vehicles that can both tow and be towed (like train cars or road trains).
Fields
attached
Indicates whether this trailer is attached to a towing vehicle.
Declaration
[Tooltip("True if object is trailer and is attached to a towing vehicle and also true if towing vehicle and has trailer\r\nattached.")]
public bool attached
Field Value
| Type | Description |
|---|---|
| bool |
Remarks
True when successfully connected to a TrailerHitchModule via physics joint. Updated automatically during attachment and detachment operations.
attachmentLayer
Physics layer for the attachment point trigger.
Declaration
[Tooltip("The layer of the SphereCollider used to detect if the trailer hitch module is in range.")]
public int attachmentLayer
Field Value
| Type | Description |
|---|---|
| int |
Remarks
Must match the attachmentLayer setting on TrailerHitchModule for detection. The trigger collider at the attachment point will be set to this layer. Use a dedicated layer to prevent false detections.
attachmentPoint
Transform representing the trailer's coupling point.
Declaration
[Tooltip("If the vehicle is a trailer, this is the object placed at the point at which it will connect to the towing vehicle. If the vehicle is towing, this is the object placed at point at which trailer will be coneected.")]
public Transform attachmentPoint
Field Value
| Type | Description |
|---|---|
| Transform |
Remarks
Position where the trailer connects to the towing vehicle's hitch. This point will be aligned with the hitch's attachment point when connected. Must be a child of the GameObject containing the TrailerModule.
Typically positioned at the trailer's tongue, fifth wheel pin, or drawbar connection.
attachmentTriggerRadius
Radius of the trigger zone at the attachment point.
Declaration
[Tooltip("The radius of the SphereCollider used to detect if the trailer hitch module is in range.")]
public float attachmentTriggerRadius
Field Value
| Type | Description |
|---|---|
| float |
Remarks
Size of the SphereCollider trigger that TrailerHitchModule detects. Should be smaller than the hitch's trigger radius to ensure proper detection hierarchy.
Typical values: - 0.1-0.15: Small precise coupler - 0.2-0.25: Standard trailer coupling - 0.3-0.4: Large industrial coupling
onAttach
Event triggered when the trailer is attached to a hitch.
Declaration
[Tooltip("Called when the trailer is attached to a hitch.")]
public UnityEvent onAttach
Field Value
| Type | Description |
|---|---|
| UnityEvent |
Remarks
Subscribe to this event to handle attachment logic like disabling autonomous systems, updating UI, or playing connection sounds.
onDetach
Event triggered when the trailer is detached from a hitch.
Declaration
[Tooltip("Called when the trailer is detached from a hitch.")]
public UnityEvent onDetach
Field Value
| Type | Description |
|---|---|
| UnityEvent |
Remarks
Subscribe to this event to handle detachment logic like enabling parking brakes, updating UI, or playing disconnection sounds.
resetInputStatesOnDetach
Reset input states when the trailer is detached.
Declaration
[Tooltip("Should the trailer input states be reset when trailer is detached?")]
public bool resetInputStatesOnDetach
Field Value
| Type | Description |
|---|---|
| bool |
Remarks
When true, clears all input states (throttle, brakes, steering) on detachment. Prevents the trailer from continuing with the last received inputs. Set to false if you want the trailer to maintain its state after detachment.
staticHoldBypassMode
When the trailer wheels release their static-friction hold. OnRigMotion keeps the hold while the rig is stationary (so it holds on a slope under handbrake) and releases when moving or launching. OnTractorInput releases on any tractor throttle/brake/handbrake (original behaviour).
Declaration
[Tooltip("When trailer wheels drop their static hold. OnRigMotion holds while stationary (holds on slopes), releases when moving/launching. OnTractorInput = original (any tractor input releases).")]
public TrailerStaticHoldBypassMode staticHoldBypassMode
Field Value
| Type | Description |
|---|---|
| TrailerStaticHoldBypassMode |
synchronizeGearShifts
Synchronize gear shifts with the towing vehicle.
Declaration
[Tooltip("If enabled the trailer will keep in same gear as the tractor, assuming powertrain on trailer is enabled.")]
public bool synchronizeGearShifts
Field Value
| Type | Description |
|---|---|
| bool |
Remarks
When enabled, the trailer's transmission will shift to match the towing vehicle's gear. Useful for powered trailers or articulated vehicles where both sections have drivetrains.
Requires both vehicles to have the same number of forward and reverse gears. The trailer's powertrain must be enabled for this to have any effect.
trailerHitch
Reference to the hitch module this trailer is attached to.
Declaration
[NonSerialized]
public TrailerHitchModule trailerHitch
Field Value
| Type | Description |
|---|---|
| TrailerHitchModule |
Remarks
Set when attached, null when detached. Use this to access the towing vehicle's controller and state information.
trailerStand
GameObject representing the trailer's support stand or landing gear.
Declaration
[Tooltip("Object that will be disabled when trailer is attached and enabled when trailer is detached.")]
public GameObject trailerStand
Field Value
| Type | Description |
|---|---|
| GameObject |
Remarks
Automatically disabled when attached and enabled when detached. Prevents the trailer from tipping forward when not connected. Typically used for semi-trailers with front landing gear.
Can be null for trailers with multiple axles that are stable when detached.
Methods
OnAttach(TrailerHitchModule)
Called when the trailer is attached to a hitch.
Declaration
public void OnAttach(TrailerHitchModule trailerHitch)
Parameters
| Type | Name | Description |
|---|---|---|
| TrailerHitchModule | trailerHitch | The hitch module attaching to this trailer |
Remarks
Handles attachment logic including disabling auto input, raising the trailer stand, setting up enable/disable listeners, and triggering the onAttach event.
The trailer's enable state is synchronized with the towing vehicle to ensure proper activation and deactivation.
OnDetach()
Called when the trailer is detached from a hitch.
Declaration
public void OnDetach()
Remarks
Handles detachment logic including resetting input states (if configured), lowering the trailer stand, removing listeners, and triggering the onDetach event.
The trailer is disabled after detachment to prevent unnecessary processing when not in use.
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.