NWH Vehicle Physics 2
Search Results for

    Show / Hide Table of Contents

    Class GroundDetection

    Detects surface type under each wheel using tags (priority), terrain splatmaps, or fallback. Applies per-surface friction presets, rolling resistance, and visual/audio effects. Runs via coroutine at groundDetectionInterval. Tracks dominantSurfacePreset across all wheels.

    Inheritance
    object
    VehicleComponent
    GroundDetection
    Inherited Members
    VehicleComponent.state
    VehicleComponent.vehicleController
    VehicleComponent.IsActive
    VehicleComponent.VC_Initialize(VehicleController)
    VehicleComponent.VC_FixedUpdate(float)
    VehicleComponent.VC_LoadStateFromStateSettings()
    VehicleComponent.VC_Update(float)
    VehicleComponent.UpdateLOD()
    VehicleComponent.ToggleState()
    Namespace: NWH.VehiclePhysics2.GroundDetection
    Assembly: NWH.VehiclePhysics2.dll
    Syntax
    [Serializable]
    public class GroundDetection : VehicleComponent

    Fields

    dominantSurfacePreset

    Surface preset with the most wheels in contact - for vehicle-wide effects. Null if no wheels grounded or no matches found.

    Declaration
    [NonSerialized]
    public SurfacePreset dominantSurfacePreset
    Field Value
    Type Description
    SurfacePreset

    groundDetectionInterval

    Seconds between detection updates. 0.05s: responsive (player), 0.1s: balanced (default), 0.2s: low CPU (AI). Randomized ±20% at init to prevent sync between vehicles.

    Declaration
    [Tooltip("Interval in seconds between ground detection updates. Lower values = more responsive, higher CPU usage.")]
    public float groundDetectionInterval
    Field Value
    Type Description
    float

    groundDetectionPreset

    Preset containing SurfaceMaps (tags/textures to surfaces), fallback preset, and particle prefabs. Multiple vehicles can share one preset. Required for ground detection.

    Declaration
    [Tooltip("Ground detection preset containing surface maps and fallback settings.")]
    public GroundDetectionPreset groundDetectionPreset
    Field Value
    Type Description
    GroundDetectionPreset

    Methods

    GetCurrentSurfaceMap(WheelController, ref int, ref SurfacePreset)

    Gets surface for a wheel: checks tags first, then terrain splatmap, then fallback. Called by the detection coroutine for each wheel.

    Declaration
    public void GetCurrentSurfaceMap(WheelController wheelController, ref int surfaceIndex, ref SurfacePreset outSurfacePreset)
    Parameters
    Type Name Description
    WheelController wheelController

    Wheel to check

    int surfaceIndex

    Matched surface map index, or -1 for fallback

    SurfacePreset outSurfacePreset

    Matched or fallback surface preset

    GetDominantTerrainTexture(Vector3, Terrain)

    Returns the terrain layer index with highest weight at the given world position. Samples splatmap to find which painted texture is dominant at the wheel contact point.

    Declaration
    public int GetDominantTerrainTexture(Vector3 worldPos, Terrain terrain)
    Parameters
    Type Name Description
    Vector3 worldPos

    World position to sample

    Terrain terrain

    Terrain to sample from

    Returns
    Type Description
    int

    Dominant texture index, or -1 if sampling fails

    GetTerrainTextureComposition(Vector3, Terrain, ref float[])

    Gets the texture composition (splatmap values) at the specified world position on a terrain. Returns an array where each element represents the alpha value of the corresponding terrain texture. Used internally by GetDominantTerrainTexture to determine surface type.

    The splatmap is a texture that Unity uses to blend multiple terrain textures together. Each pixel in the splatmap contains weights for different textures. This method:

    1. Converts world position to terrain-relative coordinates
    2. Maps coordinates to splatmap UV space
    3. Samples the splatmap at that location
    4. Returns the texture weights as a 1D array

    Array indices correspond to terrain texture order (0 = first texture, 1 = second texture, etc.). Values range from 0 to 1, with all values summing to 1.0.

    Declaration
    public void GetTerrainTextureComposition(Vector3 worldPos, Terrain terrain, ref float[] cellMix)
    Parameters
    Type Name Description
    Vector3 worldPos

    World position to sample

    Terrain terrain

    Terrain to sample from

    float[] cellMix

    Output array receiving texture alpha values for each terrain texture

    VC_Disable(bool)

    Disables the component and stops updates. Override to add component-specific disable logic.

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

    True if called by parent component or LOD system

    Returns
    Type Description
    bool

    True if successfully disabled, false otherwise

    Overrides
    VehicleComponent.VC_Disable(bool)
    Remarks

    Component must be initialized before it can be disabled. Tracks whether disable was called by parent to manage LOD system interaction.

    VC_DrawGizmos()

    Draws debug gizmos in the Scene view when the vehicle is selected. Override to visualize component-specific debug information.

    Declaration
    public override void VC_DrawGizmos()
    Overrides
    VehicleComponent.VC_DrawGizmos()
    Remarks

    Only called in Editor when the component is enabled. Use Gizmos and Handles API to draw debug visualizations.

    VC_Enable(bool)

    Enables the component and starts updates. Override to add component-specific enable logic.

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

    True if called by parent component or LOD system

    Returns
    Type Description
    bool

    True if successfully enabled, false otherwise

    Overrides
    VehicleComponent.VC_Enable(bool)
    Remarks

    Will initialize the component if not already initialized. Components disabled by LOD system will track the parent caller to prevent manual enabling while LOD is controlling state.

    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
    VehicleComponent.VC_Initialize()
    Remarks

    Override this to set up component-specific resources, cache references, or perform one-time initialization. Sets state.initialized to true on completion.

    VC_SetDefaults()

    Resets the component to default configuration. Override to provide component-specific default values.

    Declaration
    public override void VC_SetDefaults()
    Overrides
    VehicleComponent.VC_SetDefaults()
    Remarks

    Called when the vehicle is reset or when first added to a GameObject. Should set all serialized fields to sensible default values. May load default assets from Resources folder.

    VC_Validate(VehicleController)

    Validates the component configuration and reports any setup issues. Override to implement component-specific validation rules.

    Declaration
    public override void VC_Validate(VehicleController vc)
    Parameters
    Type Name Description
    VehicleController vc

    Parent VehicleController to validate against

    Overrides
    VehicleComponent.VC_Validate(VehicleController)
    Remarks

    Called in Editor when validation is triggered manually or automatically. Should use Debug.LogWarning/Error to report configuration problems. Check for null references, invalid values, missing dependencies, etc.

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