NWH Common
Search Results for

    Input

    NWH Common provides scene-level input for camera switching, camera movement, vehicle switching, character movement, and demo UI. Product-specific controls such as aircraft, ship, or road-vehicle input use provider types from those products.

    All current Common input types are in the NWH.Common.Input namespace.

    Provider model

    Every provider derives from InputProvider. Its Awake() method adds the component to the static InputProvider.Instances registry, and OnDestroy() removes it. The registry is cleared at the start of each play session, so it survives a domain reload being turned off in the editor.

    InputProvider.InstanceCount is a protected read-only count of Instances, kept for provider subclasses written against 13.x.

    SceneInputProviderBase defines these scene-level inputs:

    Method Return value
    ChangeCamera() Camera-change button state
    CameraRotation() Two-dimensional camera rotation
    CameraPanning() Two-dimensional camera panning
    CameraRotationModifier() Rotation-modifier state
    CameraPanningModifier() Panning-modifier state
    CameraZoom() Zoom value
    ChangeVehicle() Vehicle-change button state
    CharacterMovement() Two-dimensional character movement
    ToggleGUI() Demo-UI toggle state

    InputProvider.CombinedInput<T>() filters the registry to active, enabled providers of type T. Integer, float, and Vector2 overloads add all matching values; the Boolean overload returns true when any matching provider returns true. Duplicate providers therefore duplicate their numeric contribution.

    Input System provider

    The current package manifest declares Input System 1.11.2.

    1. In Edit > Project Settings > Player, set Active Input Handling to Input System Package (New), or Both when the project also needs a legacy provider.
    2. Add one InputSystemSceneInputProvider to an active scene GameObject.
    3. Configure requireCameraRotationModifier and requireCameraPanningModifier as needed.

    In Awake(), the provider constructs and enables its generated SceneInputActions wrapper. In OnDestroy(), it disables and disposes that wrapper. There is no Input Action Asset field or Auto-Enable Input option in the inspector.

    Sensitivity that has to differ per device is set on the binding, not in the provider. Mouse scroll reports 120 per notch, so the CameraZoom mouse binding carries a Scale(factor=0.01) processor while the gamepad d-pad composite has its own. CameraZoom() returns the action value unmodified.

    The shipped action asset and C# wrapper are intentionally frozen so importing the package cannot rewrite package-cache source when projects resolve different supported Input System versions. For normal project customization, use the Input System's runtime rebinding APIs or create a project-owned action asset and provider instead of editing a registry package cache.

    Package maintainers who change SceneInputActions.inputactions in a writable embedded package must temporarily enable Generate C# Class, save and regenerate the wrapper, review the generated source and XML documentation, and disable generation again before redistributing the package.

    Legacy Input Manager provider

    InputManagerSceneInputProvider supports existing projects that use Unity's legacy Input Manager. Add one provider to the scene and define the axes and buttons it reads in Edit > Project Settings > Input Manager.

    The provider reads these names:

    • Buttons: ChangeCamera, CameraRotationModifier, CameraPanningModifier, ChangeVehicle, and ToggleGUI.
    • Axes: CameraRotationX, CameraRotationY, CameraPanningX, CameraPanningY, CameraZoom, FPSMovementX, and FPSMovementY.

    A missing button is not fatal. The provider falls back to a fixed key and logs a warning once: C for ChangeCamera, V for ChangeVehicle, Tab for ToggleGUI, left mouse for CameraRotationModifier and right mouse for CameraPanningModifier. There is no such fallback for axes - a missing axis warns once and reads 0 from then on, so camera rotation, panning, zoom and character movement stay dead until the axis is defined.

    The Common package still declares Input System even when this legacy provider is used.

    Mobile provider

    MobileSceneInputProvider exposes changeCameraButton and changeVehicleButton fields for MobileInputButton components. Its other scene inputs return neutral values. Use a custom provider when a mobile scene also needs camera movement, zoom, character movement, or GUI input.

    Reading combined input

    The generic argument must be the provider base or concrete provider type whose instances should contribute:

    using NWH.Common.Input;
    using UnityEngine;
    
    public sealed class SceneInputReader : MonoBehaviour
    {
        private void Update()
        {
            Vector2 movement = InputProvider.CombinedInput<SceneInputProviderBase>(
                provider => provider.CharacterMovement());
            bool changeVehicle = InputProvider.CombinedInput<SceneInputProviderBase>(
                provider => provider.ChangeVehicle());
    
            if (changeVehicle)
            {
                Debug.Log($"Change vehicle requested; movement was {movement}.");
            }
        }
    }
    

    When a specific provider component is already available, call that component's method directly instead of querying the registry.

    Creating a custom provider

    Override only the values supplied by the new input source. The base implementations return neutral values.

    using NWH.Common.Input;
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public sealed class CustomSceneInputProvider : SceneInputProviderBase
    {
        public override bool ChangeCamera()
        {
            return Keyboard.current != null &&
                   Keyboard.current.cKey.wasPressedThisFrame;
        }
    
        public override Vector2 CameraRotation()
        {
            return Mouse.current == null
                ? Vector2.zero
                : Mouse.current.delta.ReadValue();
        }
    }
    

    Add the custom provider once to an active scene GameObject. If another active scene provider remains registered, both values participate in CombinedInput<SceneInputProviderBase>(). Disable its component or GameObject to exclude it temporarily; destroying it removes it from InputProvider.Instances.

    Troubleshooting

    Input System controls return neutral values

    • Confirm that Active Input Handling includes the Input System.
    • Confirm that an active InputSystemSceneInputProvider exists.
    • Inspect the shipped action asset and the device state in Window > Analysis > Input Debugger.

    Legacy controls return neutral values

    Confirm that the legacy Input Manager contains the exact axis and button names listed above. Buttons keep working on their fallback keys, so a silent failure here is almost always a missing axis rather than a missing button.

    Input is too large or a button fires unexpectedly

    Inspect InputProvider.Instances for duplicate providers. Numeric values from every matching provider are added, while Boolean values are ORed.

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