NWH Vehicle Physics 2
Search Results for

    Show / Hide Table of Contents

    Vehicle Component

    VehicleComponent is the abstract base class for all vehicle subsystems and modules in NWH Vehicle Physics 2. All major vehicle systems inherit from this class, including VehicleModules, Effects, and SoundComponents.

    Architecture

    VehicleController manages a collection of VehicleComponents. Each component follows a standardized lifecycle with methods prefixed VC_ to distinguish them from Unity callbacks.

    Lifecycle

    Components follow this initialization and update sequence:

    1. VC_Initialize(VehicleController) - Sets vehicle controller reference
    2. VC_Initialize() - Initializes component internals
    3. VC_LoadStateFromStateSettings() - Loads enabled/LOD settings from StateSettings
    4. VC_Enable() / VC_Disable() - Activates/deactivates the component
    5. VC_FixedUpdate(float) / VC_Update(float) - Called each physics/frame update when active

    Key Properties

    • IsActive - Returns true when component is both initialized and enabled. Components only update when active.
    • state - StateDefinition containing enabled status, LOD index, and initialization state.
    • vehicleController - Reference to parent VehicleController.

    Virtual Methods

    Lifecycle Methods

    • VC_Initialize(VehicleController) - Sets the VehicleController reference. Called first during initialization.
    • VC_Initialize() - Initializes component-specific systems and resources. Override to set up your component.
    • VC_SetDefaults() - Resets component to default configuration. Called when vehicle is reset or component is first added.
    • VC_LoadStateFromStateSettings() - Loads enabled/disabled and LOD configuration from StateSettings.

    Update Methods

    • VC_FixedUpdate(float) - Physics update at fixed intervals. Use for physics calculations and force application.
    • VC_Update(float) - Frame update. Use for visual effects and input handling.

    State Management

    • VC_Enable(bool calledByParent) - Enables the component and starts updates. Initializes if not already initialized.
    • VC_Disable(bool calledByParent) - Disables the component and stops updates.
    • UpdateLOD() - Updates enabled state based on current LOD level. Called automatically by LOD system.
    • ToggleState() - Toggles between enabled and disabled states.

    Editor Methods

    • VC_Validate(VehicleController) - Validates component configuration. Override to check for setup issues.
    • VC_DrawGizmos() - Draws debug visualizations in Scene view.

    State Management

    Enabled / Disabled

    • Enabled components are updated each frame/physics step.
    • Disabled components are not initialized until enabled for the first time.
    • Disabling a ManagerVehicleComponent (e.g., SoundManager, EffectsManager, ModuleManager, Powertrain) also disables its child components.

    LOD System

    Components support automatic enabling/disabling based on distance from camera:

    • lodIndex >= 0 - Component controlled by LOD system. Enabled when vehicleController.activeLODIndex <= state.lodIndex.
    • lodIndex < 0 - LOD system disabled. Component uses manual enabled/disabled state.

    LODs are configured through StateSettings and can be previewed using the state bar in play mode.

    State Settings

    StateSettings is a ScriptableObject that stores enabled/disabled and LOD configuration for all components. This prevents having to configure each component on every vehicle.

    • Assign StateSettings under VehicleController => Settings tab.
    • See State Settings for details.

    Scripting Examples

    Enabling/Disabling Components

    // Disable LOD control and manually enable component
    myVehicleComponent.state.lodIndex = -1;
    myVehicleComponent.VC_Enable(false);
    
    // Disable component
    myVehicleComponent.VC_Disable(false);
    
    // Toggle state
    myVehicleComponent.ToggleState();
    
    // Check if component is operational
    if (myVehicleComponent.IsActive)
    {
        // Component is initialized and enabled
    }
    

    Creating Custom Components

    public class MyCustomComponent : VehicleComponent
    {
        protected override void VC_Initialize()
        {
            base.VC_Initialize();
            // Initialize your component
        }
    
        public override void VC_FixedUpdate(float deltaTime)
        {
            if (!IsActive) return;
            // Physics calculations here
        }
    
        public override void VC_SetDefaults()
        {
            base.VC_SetDefaults();
            // Set default values
        }
    }
    
    In this article
    Back to top Copyright © NWH - Vehicle Physics, Aerodynamics, Dynamic Water Physics