User Tools


Input

  • Vehicles retrieve user input through Input class which retrieves input from active InputProvider and fills the InputStates struct with the retrieved data.
  • Multiple different InputProviders can be present in the scene (v1.0 or newer required). E.g. InputSystemProvider and MobileInputProvider can be used in the same scene. The resulting input will be a sum of inputs from all InputProviders in case of numeric inputs and logical OR operation of all inputs in case of boolean inputs.
  • Input is stored inside InputStates object and can be copied over from one vehicle to another. E.g. this is what is done when a trailer is connected to a towing vehicle.
  • To manually set the InputStates make sure Auto Settable is set to false.


All input providers inherit from abstract class InputProvider but have different implementations:

Check the Scripting section below on how to make a custom InputProvider or set up AI.

Input System Warning

When importing the asset for the first time this message will pop up:

To use only the new InputSystem in the project select Yes. Otherwise, to use the old/classic Unity Input (InputManager) click No and then under Project Settings ⇒ Player ⇒ Input Handling select Both. Otherwise this message will keep popping up each time the project is opened.

Available Bindings

Out of the box gamepad bindings are only available for InputSystem.

Name Type Keyboard Defaults Gamepad Defaults Description
Vehicle Bindings
Steering axis [-1,1] A/D Left Stick - Left/Right Steering.
Throttle axis [0,1] W Left Stick - Up, Right Trigger Throttle.
Brakes axis [0,1] S Left Stick - Down, Left Trigger Brakes.
Clutch axis [0,1] Manual clutch. 0 for disengaged and 1 for engaged.
Handbrake axis [0,1] Space B (Xbox) / Circle (PS)
EngineStartStop Button E
ShiftUp button R Right Shoulder
ShiftDown button F Left Shoulder
ShiftIntoR1 button ` Shift into 1st reverse gear.
ShiftIntoN button 0 Shift into neutral.
ShiftInto1 button 1 Shift into 1st forward gear.
ShiftInto[n] button 2,3,4,etc. Shift into [n]th gear.
LowBeamLights button L Y (Xbox) / Triangle (PS)
HighBeamLights button K
HazardLights button J
ExtraLights button ;
LeftBlinker button Z
RightBlinker button X
Horn button H
Module Bindings
FlipOver button M Used for FlipOverModule.
Boost button Left Shift A (Xbox) / X (PS) Used for NOSModule.
Cruise Control button N Used for CruiseControlModule.
TrailerAttachDetach button T X (Xbox) / Square (PS) Used for Trailer and TrailerHitch modules.
Camera Bindings
ChangeCamera button C Start Changes camera.
CameraRotation 2D axis Mouse Delta Right Stick Controls camera rotation.
CameraPanning 2D axis Mouse Delta Right Stick Controls camera panning.
CameraRotationModifier button Mouse - LMB Right Stick Press Enables camera rotation.
CameraPanningModifier button Mouse - RMB Left Stick Press Enables camera panning.
CameraZoom axis Mouse - Scroll D-Pad Up/Down Camera zoom in/out.
Scene Bindings
ChangeVehicle button V Select Change vehicle or enter/exit vehicle.
FPSMovement 2D axis WASD Left Stick Demo FPS controller movement.
ToggleGUI button Tab Toggles demo scene GUI.

Input Manager (old/classic)

  • Type of Input Provider for handling user input on desktop devices through keyboard and mouse or gamepad.
  • Uses classic/old Unity Input Manager. It is recommended to use the InputSystemProvider instead which uses new Unity Input System - Input System Provider page.


Since v1.1 InputSystem package is required even if not used. If using the old/classic Unity input set Project Settings ⇒ Player ⇒ Input Handling to Both and proceed as normal. InputSystem package being present installed will not interfere with old/classic Unity input / InputManager.

Setup

When first importing NWH Vehicle Physics 2 the project will be missing required bindings. There are two ways to add those:

  1. Manually adding each entry to the Project Settings ⇒ Input following the input bindings table available here.
  2. Copying the contents of InputBindings.txt and appending them to the contents of the [UnityProjectPath]/ProjectSettings/InputManager.asset file. To do so:
    • Close Unity.
    • Open InputManager.asset in Notepad/Notepad++/Visual Studio or any other text editor of your choice.
    • Copy the contents of the provided InputBindings.txt file (Scripts ⇒ Vehicle ⇒ Control ⇒ Input ⇒ InputProviders ⇒ ClassicUnityInput ⇒ InputBindings.txt) and paste them at the end of the InputManager.asset. Make sure there are no empty lines between the existing content and the pasted content. Save the file.
    • Open Unity. Check Project Settings ⇒ Input. The input bindings for NWH Vehicle Physics will appear towards the bottom of the list.
2020/04/08 12:36

Input System (new)

Default InputActions.
  • Since v1.1 NWH Vehicle Physics 2 has moved to InputSystem as a default input method.
  • InputSystem v1.0 or higher is required. This is available in Unity 2019.3 or newer.

When using DS4Windows, InputSystem will detect button presses twice.

Setup

  • Install 'Input System' package through Window ⇒ Package Manager
  • Under Edit ⇒ Project Settings ⇒ Player ⇒ Other Settings ⇒ Active Input Handling select Input System Package (New) or Both - the latter in case your project still uses UnityEngine.Input somewhere.
  • Add InputSystemProvider to any object in your scene. It is best to remove DesktopInputManager if it is present in the scene.
  • InputSystem is now ready to go.
  • Default bindings can be modified by double clicking on VehicleInputActions file. The file is located in Scripts/Vehicle/Control/Input/InputProviders/InputSystemProvider. Save Asset must be clicked for the changes to take effect.
2020/06/16 11:32 · Aron Rescec

Mobile Input Provider

  • Add MobileInputProvider to the scene.
  • Create a few UI ⇒ Button objects inside your canvas. Make sure that they are clickable.
  • Remove the UnityEngine.UI.Button component and replace it with MobileInputButton. MobileInputButton inherits from UnityEngine.UI.Button and adds hasBeenClicked and isPressed fields which are required for Mobile Input Provider
  • Drag the buttons to the corresponding fields in the MobileInputProvider. Empty fields will be ignored.

Steering Wheel Input Provider

For more info visit SteeringWheelInputProvider page.

Scripting

Retrieving Input

Since v1.0 multiple InputProviders can be present in the scene, meaning that their input has to be combined to get the final input result. To get the combined input use:

float throttle = InputProvider.CombinedInput(i => i.Throttle());
bool engineStartStop = InputProvider.CombinedInput(i => i.EngineStartStop());

Or to get the input from individual InputProviders (say to find out if a button was pressed on a keyboard): <code>float throttle = InputProvider.Instances[0].Throttle;</code> When using input generated by code (i.e. AI) it is usually handy to have access to a single axis throttle/brake. This can be done like so:

vehicleController.input.Vertical = 0.5f; //Sets throttle to 0.5f, resets brakes.
vehicleController.input.Vertical = -0.5f; //Sets brakes to 0.5f, resets throttle.

vehicleController.input.states.throttle is equal to vehicleController.input.Throttle. The latter is just a getter/setter for convenience.

Manually Setting Input

Input in each vehicle is stored in InputStates struct:

myVehicleController.input.states

In case input should not be retrieved from user but from another script - as is the case when AI is used - AutoSettable should be set to false. This will disable automatic input fetching from the active InputProvider.
Input now can be set from any script:

myVehicleController.input.Horizontal = myFloatValue; // Using getter/setter.
myVehicleController.input.states.horizontal = myFloatValue; // Directly accessing states.
Custom InputProvider

If a custom InputProvider is needed it can easily be written. Custom InputProviders allow for new input methods or for modifying the existing ones. E.g. if the MobileInputProvider does not fit the needs of the project a copy of it can be made and modifications done on that copy. That way it will not get overwritten when the asset is updated.

Steps to create a new InputProvider:

  • Create a new class, e.g. ExampleInputProvider and make it inherit from InputProvider class:
public class ExampleInputProvider : InputProvider {}
  • Implement missing methods. Most IDEs can do this automatically.
  • The required methods are abstract and will need to be implemented. There are also virtual methods such as ToggleGUI() which are optional and will be ignored if not implemented.
  • Methods that are not used should return false, 0 or -999 in case of ShiftInto() method.