Class Powertrain
Main powertrain manager that coordinates the entire drivetrain system from engine to wheels.
Inherited Members
Namespace: NWH.VehiclePhysics2.Powertrain
Assembly: NWH.VehiclePhysics2.dll
Syntax
[Serializable]
public class Powertrain : ManagerVehicleComponent
Remarks
The Powertrain class manages all powertrain components and their interconnections, forming a complete drivetrain simulation from the engine through clutch, transmission, differentials, and finally to the wheels. It handles component initialization, physics updates, and the torque flow through the entire system.
The powertrain architecture follows a graph-based structure where components are linked together in a chain. Torque flows from the engine (power source) through the clutch and transmission, then splits through differentials to reach the wheels (power sink). Each component contributes its inertia and losses to the overall system behavior.
Key responsibilities: - Managing engine, clutch, transmission, and differential components - Organizing wheels into logical groups for steering, braking, and differential connections - Coordinating physics updates through the powertrain solver - Handling component validation and default setup - Providing damage simulation and repair functionality
The powertrain solver uses a two-pass approach: first, angular velocities are queried backward from wheels to engine to determine component speeds; second, torque is propagated forward from engine to wheels to calculate forces. This bidirectional solving ensures accurate simulation of complex drivetrain interactions.
Fields
clutch
Clutch component that manages power transmission between engine and transmission.
Declaration
public ClutchComponent clutch
Field Value
| Type | Description |
|---|---|
| ClutchComponent |
Remarks
Controls engagement/disengagement and slip characteristics. Acts as the connection point between the engine's rotational output and the transmission input.
differentials
List of differential components that distribute torque between outputs (wheels or other differentials).
Declaration
public List<DifferentialComponent> differentials
Field Value
| Type | Description |
|---|---|
| List<DifferentialComponent> |
Remarks
Differentials allow wheels to rotate at different speeds during cornering while distributing torque. A typical vehicle setup includes front, rear, and center differentials connected in a hierarchy.
Common configurations: - 2WD: Single differential (front or rear) connected to two wheels - AWD: Three differentials - center splits power to front and rear, which each split to left/right wheels - 4WD: Similar to AWD but with different torque distribution characteristics
engine
Engine component that generates rotational power from throttle input.
Declaration
public EngineComponent engine
Field Value
| Type | Description |
|---|---|
| EngineComponent |
Remarks
The power source of the vehicle. Converts fuel/electric energy into rotational torque based on RPM, throttle position, and power curves. Includes starting, idle control, and rev limiting.
transmission
Transmission component that adjusts gear ratios to optimize power delivery.
Declaration
public TransmissionComponent transmission
Field Value
| Type | Description |
|---|---|
| TransmissionComponent |
Remarks
Multiplies engine torque through gear ratios to provide appropriate power at different speeds. Handles automatic or manual shifting, gear selection logic, and shift timing.
wheelCount
Total number of wheels in the powertrain system.
Declaration
public int wheelCount
Field Value
| Type | Description |
|---|---|
| int |
Remarks
Cached during initialization for performance. Includes all wheels connected to the powertrain regardless of whether they are powered, steered, or braked.
wheelGroupCount
Total number of wheel groups (axles) in the vehicle.
Declaration
public int wheelGroupCount
Field Value
| Type | Description |
|---|---|
| int |
Remarks
Each group typically represents an axle with shared properties like steering coefficient, brake distribution, and handbrake application.
wheelGroups
List of wheel groups that organize wheels into logical axles for steering, braking, and other control functions.
Declaration
public List<WheelGroup> wheelGroups
Field Value
| Type | Description |
|---|---|
| List<WheelGroup> |
Remarks
Wheel groups organize wheels that should share certain characteristics. Each group defines: - Steering coefficient (how much steering input affects this axle) - Brake coefficient (brake force distribution) - Handbrake coefficient (handbrake application strength) - Ackermann steering geometry settings
Typical setup: Front group (steering=1.0, brake=1.0), Rear group (steering=0, brake=0.7, handbrake=1.0). Multi-axle vehicles like trucks may have additional middle groups with intermediate values.
wheels
List of all wheel components in the powertrain, ordered front-to-back and left-to-right.
Declaration
public List<WheelComponent> wheels
Field Value
| Type | Description |
|---|---|
| List<WheelComponent> |
Remarks
Wheels are the final power sink in the powertrain chain. They convert rotational torque into linear motion through tire-ground interaction, and also provide resistance torque back to the engine.
Ordering convention: wheels are sorted by Z position (front first) then X position (left first). Example 4-wheel order: Front-Left, Front-Right, Rear-Left, Rear-Right. This ordering is important for consistent setup and differential connections.
Methods
FillComponentList()
Override to populate _components with your child components. Called lazily on first Components access.
Declaration
protected override void FillComponentList()
Overrides
FillInWheels()
Automatically finds and assigns WheelController components to the powertrain's wheel list.
Declaration
public void FillInWheels()
Remarks
Searches for all WheelController components in the vehicle hierarchy, sorts them in standard order (front-to-back, left-to-right), and assigns them to the existing wheel component slots. The number of found WheelControllers should match the wheelCount for proper operation. Used during vehicle setup to automatically connect wheels to the powertrain system.
GetSortedWheels(VehicleController)
Gets all WheelController components from the vehicle and sorts them in standard order.
Declaration
public static List<WheelController> GetSortedWheels(VehicleController vehicleController)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vehicleController | The vehicle controller to search for wheel controllers. |
Returns
| Type | Description |
|---|---|
| List<WheelController> | List of WheelController components sorted front-to-back (Z descending) then left-to-right (X ascending). |
Remarks
The sorting order ensures consistent wheel identification and connection throughout the powertrain system. Wheels are first sorted by their local Z position (front wheels first), then by local X position (left wheels first).
Standard 4-wheel order: Front-Left, Front-Right, Rear-Left, Rear-Right. This ordering convention is important for proper differential connections and wheel group assignments.
Inspector_GetPowertrainComponentFromName(string)
Finds a powertrain component by its name string.
Declaration
public PowertrainComponent Inspector_GetPowertrainComponentFromName(string componentName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | componentName | Name of the component to search for. |
Returns
| Type | Description |
|---|---|
| PowertrainComponent | The PowertrainComponent with matching name, or null if not found. |
Remarks
Preferred over Inspector_GetPowertrainComponentFromNameHash(int) for persisted link reconstruction since string comparison is stable across Unity versions while string.GetHashCode is not.
Inspector_GetPowertrainComponentFromNameHash(int)
Finds a powertrain component by its name hash code.
Declaration
public PowertrainComponent Inspector_GetPowertrainComponentFromNameHash(int nameHash)
Parameters
| Type | Name | Description |
|---|---|---|
| int | nameHash | The hash code of the component's name to search for. |
Returns
| Type | Description |
|---|---|
| PowertrainComponent | The PowertrainComponent with matching name hash, or null if not found. |
Remarks
Uses hash code comparison for fast component lookups when establishing connections between powertrain components. This is primarily used during initialization to restore serialized connections.
Inspector_GetPowertrainComponentNames()
Gets a list of all powertrain component names for inspector dropdown menus.
Declaration
public List<string> Inspector_GetPowertrainComponentNames()
Returns
| Type | Description |
|---|---|
| List<string> | List of component names starting with "[none]" followed by engine, clutch, transmission, differentials, and wheels. |
Remarks
This method is intended for inspector/editor use only as it performs string allocation which is not optimal for runtime. Used to populate dropdown menus when connecting powertrain components in the editor.
Inspector_GetPowertrainComponents()
Gets a list of all powertrain component references for inspector operations.
Declaration
public List<PowertrainComponent> Inspector_GetPowertrainComponents()
Returns
| Type | Description |
|---|---|
| List<PowertrainComponent> | List of PowertrainComponent references with null as the first element, followed by all components. |
Remarks
The first element is intentionally null to correspond with the "[none]" option in component name dropdowns. This allows index-based matching between component names and component references in the inspector.
Repair()
Repairs all powertrain components by resetting their damage values to zero.
Declaration
public void Repair()
Remarks
Instantly restores all powertrain components to full functionality. This includes the engine, clutch, transmission, all differentials, and all wheels. Useful for implementing repair mechanics, respawning vehicles, or resetting vehicle state.
VC_FixedUpdate(float)
Physics update for manager and all active children.
Declaration
public override void VC_FixedUpdate(float DeltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | DeltaTime |
Overrides
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_Initialize(VehicleController)
Initializes manager and all children with VehicleController reference.
Declaration
public override void VC_Initialize(VehicleController vc)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vc |
Overrides
VC_SetDefaults()
Resets manager and all children to defaults.
Declaration
public override void VC_SetDefaults()
Overrides
VC_Validate(VehicleController)
Validates manager and all enabled children. Skips if manager is disabled.
Declaration
public override void VC_Validate(VehicleController vc)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vc |