Class TrailerHitchModule
Tow hitch for vehicles pulling a TrailerModule. Builds the physics joint between the two vehicles at their attachment points and syncs the trailer's input and lights.
Inherited Members
Namespace: NWH.VehiclePhysics2.Modules.Trailer
Assembly: NWH.VehiclePhysics2.dll
Syntax
[Serializable]
public class TrailerHitchModule : VehicleComponent
Remarks
A SphereCollider trigger is created at the attachment point on initialization. Once a TrailerModule coupler is inside it, the attach/detach input (default 'T') connects the trailer and both attachment points are aligned. One-shot overlap queries run on enable and on attach input, so couplers already overlapping or spawned at runtime are picked up without a public rescan call.
Fields
attachOnEnable
Connects a trailer that is already inside the trigger radius when the module enables, with no input needed. For scenes that start with the rig hitched up.
Declaration
[FormerlySerializedAs("attachOnPlay")]
[Tooltip("If a trailer is in range when the scene is started it will be attached.")]
public bool attachOnEnable
Field Value
| Type | Description |
|---|---|
| bool |
attached
True while the physics joint is connected. Also true on a trailer that has a tractor attached to it.
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 |
attachedTrailerModule
TrailerModule of the attached trailer, null when nothing is attached. Its vehicleController is the way through to the trailer itself.
Declaration
[NonSerialized]
[Tooltip("A trailer that is attached to this trailer hitch")]
public TrailerModule attachedTrailerModule
Field Value
| Type | Description |
|---|---|
| TrailerModule |
attachmentLayer
Layer the attachment trigger scans. TrailerModule attachment points have to be on the same layer, and a layer of its own keeps unrelated colliders out.
Declaration
[Tooltip("The physics layer of the SphereCollider used to detect if the trailer module is in range.")]
public int attachmentLayer
Field Value
| Type | Description |
|---|---|
| int |
attachmentPoint
Coupling point on the vehicle, at the ball hitch. The trailer's attachment point is moved onto it and the joint is created there. Must be a child of the GameObject that carries this module.
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 |
attachmentTriggerRadius
Radius of the detection trigger at the attachment point. Larger is easier to hitch but starts accepting badly aligned trailers. 0.2 to 0.3 is tight, 0.4 to 0.5 normal, 0.6 to 1.0 very forgiving.
Declaration
[Tooltip("The radius of the SphereCollider used to detect if the trailer module is in range.")]
public float attachmentTriggerRadius
Field Value
| Type | Description |
|---|---|
| float |
breakForce
Force at which the joint breaks and the trailer detaches. Mathf.Infinity never breaks. 10000 to 50000 is light duty, 50000 to 200000 standard, 200000 to 500000 heavy duty.
Declaration
[Tooltip("Breaking force of the generated joint.")]
public float breakForce
Field Value
| Type | Description |
|---|---|
| float |
breakTorque
Moment at which the joint breaks, in Nm. Only reachable through locked angular DOFs, so it does nothing on a ball coupling where all three angular axes are free. Mathf.Infinity never breaks.
Declaration
[Tooltip("Breaking torque of the generated joint, in Nm. Only reachable through locked angular DOFs.")]
public float breakTorque
Field Value
| Type | Description |
|---|---|
| float |
detachable
Allows the trailer to be detached again. False makes the coupling permanent, though exceeding breakForce still breaks it.
Declaration
[Tooltip("Can the trailer be detached once it is attached?")]
public bool detachable
Field Value
| Type | Description |
|---|---|
| bool |
disconnectMode
Determines disconnect behavior for multi-trailer chains.
Declaration
[Tooltip("Closest: Disconnect trailer attached to this hitch. Furthest: Disconnect last trailer in chain.")]
public TrailerDisconnectMode disconnectMode
Field Value
| Type | Description |
|---|---|
| TrailerDisconnectMode |
noTrailerPowerCoefficient
Engine power multiplier while running solo. A truck geared for a full load spins its wheels on binary throttle without one. 1.0 is full power, 0.7 to 0.9 a mild cut, 0.5 to 0.7 a heavy one.
Declaration
[Tooltip("Power reduction that will be applied when vehicle has no trailer to avoid wheel spin when controlled with a binary controller.")]
public float noTrailerPowerCoefficient
Field Value
| Type | Description |
|---|---|
| float |
onTrailerAttach
Fired once a trailer is attached.
Declaration
public UnityEvent onTrailerAttach
Field Value
| Type | Description |
|---|---|
| UnityEvent |
onTrailerDetach
Fired once a trailer is detached.
Declaration
public UnityEvent onTrailerDetach
Field Value
| Type | Description |
|---|---|
| UnityEvent |
trailerInRange
True while an available TrailerModule attachment point sits inside the trigger zone, for showing an attach prompt. Recomputed from the in-range candidates every FixedUpdate.
Declaration
[Tooltip("Is trailer's attachment point close enough to be attached to the towing vehicle?")]
public bool trailerInRange
Field Value
| Type | Description |
|---|---|
| bool |
useHingeJoint
Restricts the coupling to yaw only, for articulated buses and logging rigs where the trailer must not roll relative to the tractor. False gives a ball joint free on all three angular axes.
Declaration
[Tooltip("Use for articulated buses and equipment where roll rotation should be prevented.")]
public bool useHingeJoint
Field Value
| Type | Description |
|---|---|
| bool |
Methods
AttachTrailer(TrailerModuleWrapper)
Attaches a trailer to this hitch.
Declaration
public void AttachTrailer(TrailerModuleWrapper trailerWrapper)
Parameters
| Type | Name | Description |
|---|---|---|
| TrailerModuleWrapper | trailerWrapper | The wrapper component of the trailer to attach |
Remarks
Repositions the trailer so the attachment points align, then builds a ConfigurableJoint configured from useHingeJoint. Input states and lighting are synced and onTrailerAttach is fired afterwards.
DetachFurthestTrailer()
Detaches the furthest non-connector trailer in the chain.
Declaration
public void DetachFurthestTrailer()
DetachTrailer(VehicleController)
Detaches the currently attached trailer.
Declaration
public void DetachTrailer(VehicleController vc)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vc | The vehicle controller of the towing vehicle |
Remarks
Destroys the joint and cleans up both the hitch and the trailer module, then fires onTrailerDetach. Refused unless detachable is set.
GetLastTrailerInChain()
Gets the last non-connector trailer in the chain (excludes trailers with non-detachable hitches).
Declaration
public TrailerModule GetLastTrailerInChain()
Returns
| Type | Description |
|---|---|
| TrailerModule |
GetTrailerChain(bool)
Gets all trailers in the chain, optionally excluding dollies.
Declaration
public List<TrailerModule> GetTrailerChain(bool includeDollies = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | includeDollies | If false, trailers with non-detachable hitches (connectors) are excluded |
Returns
| Type | Description |
|---|---|
| List<TrailerModule> | List of TrailerModules in order from nearest to furthest |
GetTrailerCount()
Gets count of actual trailers (excludes connectors with non-detachable hitches).
Declaration
public int GetTrailerCount()
Returns
| Type | Description |
|---|---|
| int |
NoTrailerPowerModifier(float)
Power modifier function that reduces power when no trailer is attached.
Declaration
public float NoTrailerPowerModifier(float DeltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | DeltaTime | Physics time step in seconds |
Returns
| Type | Description |
|---|---|
| float | Power coefficient to apply to engine output |
OnTriggerEnter(Collider)
Called when a collider enters the attachment trigger zone.
Declaration
public virtual void OnTriggerEnter(Collider other)
Parameters
| Type | Name | Description |
|---|---|---|
| Collider | other | The collider that entered the trigger |
OnTriggerExit(Collider)
Called when a collider exits the attachment trigger zone.
Declaration
public virtual void OnTriggerExit(Collider other)
Parameters
| Type | Name | Description |
|---|---|---|
| Collider | other | The collider that exited the trigger |
OnTriggerStay(Collider)
Called while a collider remains in the attachment trigger zone.
Declaration
public virtual void OnTriggerStay(Collider other)
Parameters
| Type | Name | Description |
|---|---|---|
| Collider | other | The collider within the trigger |
Remarks
Retained for source compatibility with subclasses. The package-owned relay deliberately does not forward OnTriggerStay; missed enters are recovered with one-shot overlap queries.
VC_Disable(bool)
Disables the component and stops its updates.
Declaration
public override bool VC_Disable(bool calledByParent)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | calledByParent | True when called by a parent ManagerVehicleComponent fan-out or the VehicleController enable/disable loop. The LOD system passes false. |
Returns
| Type | Description |
|---|---|
| bool | True if successfully disabled, false otherwise |
Overrides
Remarks
Refused on a component that is not initialized. Records whether the call came from a parent, which is what VC_Enable checks before a bulk re-enable.
VC_Enable(bool)
Enables the component and starts its updates.
Declaration
public override bool VC_Enable(bool calledByParent)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | calledByParent | True when called by a parent ManagerVehicleComponent fan-out or the VehicleController enable/disable loop. The LOD system passes false. |
Returns
| Type | Description |
|---|---|
| bool | True if successfully enabled, false otherwise |
Overrides
Remarks
Initializes the component first if it is not initialized yet. A parent bulk enable is refused unless the last disable also came from a parent, so it cannot resurrect a component turned off by LOD, by a script, or disabled in StateSettings.
VC_FixedUpdate(float)
Fixed-timestep update for forces, torques and other physics work. Called from VehicleController.FixedUpdate(), and only while the component is active.
Declaration
public override void VC_FixedUpdate(float DeltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | DeltaTime | Time elapsed since last FixedUpdate in seconds |
Overrides
VC_Initialize()
One-time setup, once per vehicle startup and after the VehicleController reference is set. Sets state.initialized on completion; an override that fails must leave it false.
Declaration
protected override void VC_Initialize()
Overrides
VC_Update(float)
Per-frame update for input, visuals and UI. Only called while the component is active. Physics belongs in VC_FixedUpdate, not here.
Declaration
public override void VC_Update(float DeltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | DeltaTime | Time elapsed since last Update in seconds |