NWH Vehicle Physics 2
Search Results for

    Show / Hide Table of Contents

    Class CruiseControlModule

    Cruise control module that automatically maintains a target vehicle speed. Uses a PID (Proportional-Integral-Derivative) controller for smooth speed regulation.

    Features:

    • Automatic speed maintenance using throttle and optionally brakes
    • PID-based control for smooth acceleration and deceleration
    • Configurable activation/deactivation behavior
    • Safety features including brake override

    The module works only in forward motion and automatically disengages when:

    • The vehicle stops or goes in reverse
    • The brake is pressed (if configured)
    • The module is manually deactivated

    Ideal for highway driving simulation, long-distance travel, or reducing player fatigue in games with extensive driving segments.

    Inheritance
    object
    VehicleComponent
    CruiseControlModule
    Inherited Members
    VehicleComponent.state
    VehicleComponent.vehicleController
    VehicleComponent.IsActive
    VehicleComponent.VC_Initialize(VehicleController)
    VehicleComponent.VC_Initialize()
    VehicleComponent.VC_SetDefaults()
    VehicleComponent.VC_LoadStateFromStateSettings()
    VehicleComponent.VC_Update(float)
    VehicleComponent.VC_DrawGizmos()
    VehicleComponent.VC_Validate(VehicleController)
    VehicleComponent.UpdateLOD()
    VehicleComponent.ToggleState()
    Namespace: NWH.VehiclePhysics2.Modules.CruiseControl
    Assembly: NWH.VehiclePhysics2.dll
    Syntax
    [Serializable]
    public class CruiseControlModule : VehicleComponent

    Fields

    Kd

    Derivative gain of the PID controller. Reduces overshooting by damping rapid changes.

    Typical values: 0.05-0.3

    • Low (0.05): Minimal damping, may overshoot
    • Medium (0.1): Balanced response
    • High (0.3): Heavy damping, very stable but slower response
    Declaration
    [Tooltip("Derivative gain of PID controller.")]
    public float Kd
    Field Value
    Type Description
    float

    Ki

    Integral gain of the PID controller. Eliminates steady-state error over time.

    Typical values: 0.1-0.5

    • Low (0.1): Slow error correction
    • Medium (0.25): Balanced correction
    • High (0.5): Fast correction, may cause oscillation

    Increase if speed settles below/above target.

    Declaration
    [Tooltip("Integral gain of PID controller.")]
    public float Ki
    Field Value
    Type Description
    float

    Kp

    Proportional gain of the PID controller. Primary response to speed error.

    Typical values: 0.3-1.0

    • Low (0.3): Gentle speed changes
    • Medium (0.5): Balanced response
    • High (1.0): Aggressive speed matching

    Higher values provide faster response but may cause hunting.

    Declaration
    [Tooltip("Proportional gain of PID controller.")]
    public float Kp
    Field Value
    Type Description
    float

    applyBrakesWhenSpeeding

    Determines if the cruise control can apply brakes when exceeding target speed. When enabled, provides more precise speed control on downhill sections. When disabled, only reduces throttle, allowing speed to increase on descents.

    Declaration
    [Tooltip("If true brakes will be applied when speeding.")]
    public bool applyBrakesWhenSpeeding
    Field Value
    Type Description
    bool

    cruiseControlActive

    Indicates whether cruise control is currently active and maintaining speed. Can be toggled via the cruise control input or set programmatically. Read this value to update UI indicators.

    Declaration
    [Tooltip("Is the cruise control currently active and trying to hold the vehicle speed?")]
    public bool cruiseControlActive
    Field Value
    Type Description
    bool

    deactivateOnBrake

    Safety feature that disengages cruise control when brakes are applied. Recommended to keep enabled for realistic behavior. Disable only for specialized gameplay mechanics.

    Declaration
    [Tooltip("If true cruise control will be disabled if brakes are activated.")]
    public bool deactivateOnBrake
    Field Value
    Type Description
    bool

    setTargetSpeedOnEnable

    Automatically sets the target speed to current speed when module is enabled. Useful for quick activation without manual speed setting. Disable if you want to preserve a previously set target speed.

    Declaration
    [Tooltip("Should the speed be set automatically when the module is enabled?")]
    public bool setTargetSpeedOnEnable
    Field Value
    Type Description
    bool

    targetSpeed

    Target speed in m/s that the cruise control will maintain. Set automatically when activated or can be adjusted manually. Typical ranges:

    • City: 10-20 m/s (36-72 km/h)
    • Highway: 25-35 m/s (90-126 km/h)
    Declaration
    [Tooltip("The speed the vehicle will try to hold.")]
    public float targetSpeed
    Field Value
    Type Description
    float

    Methods

    VC_Disable(bool)

    Disables the cruise control module and unregisters input callbacks.

    Declaration
    public override bool VC_Disable(bool calledByParent)
    Parameters
    Type Name Description
    bool calledByParent

    Whether this was called by the parent component.

    Returns
    Type Description
    bool

    True if successfully disabled.

    Overrides
    VehicleComponent.VC_Disable(bool)

    VC_Enable(bool)

    Enables the cruise control module and registers input callbacks.

    Declaration
    public override bool VC_Enable(bool calledByParent)
    Parameters
    Type Name Description
    bool calledByParent

    Whether this was called by the parent component.

    Returns
    Type Description
    bool

    True if successfully enabled.

    Overrides
    VehicleComponent.VC_Enable(bool)

    VC_FixedUpdate(float)

    Updates the cruise control PID controller and manages activation state.

    Declaration
    public override void VC_FixedUpdate(float DeltaTime)
    Parameters
    Type Name Description
    float DeltaTime

    Fixed time step in seconds.

    Overrides
    VehicleComponent.VC_FixedUpdate(float)

    See Also

    ModuleManager
    VehicleController
    EngineComponent
    Brakes
    In this article
    Back to top Copyright © NWH - Vehicle Physics, Aerodynamics, Dynamic Water Physics