Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | Setup:Input [2023/06/09 14:29] (current) – [Manually Setting Input] nwhcoding | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Input ====== | ||
+ | <wrap round important> | ||
+ | |||
+ | ===== Input Concept ===== | ||
+ | The input in NWH Vehicle Physics 2 revolves around '' | ||
+ | |||
+ | Multiple '' | ||
+ | |||
+ | ===== Input Retrieval ===== | ||
+ | [{{ : | ||
+ | |||
+ | The diagram above illustrates the path from the input source to the vehicle input state: | ||
+ | - Input is obtained from hardware through //Input Sources// (green). This can be input from any method available in Unity. In this example, InputManager (old/ | ||
+ | - // | ||
+ | - If '' | ||
+ | - If the VehicleController has '' | ||
+ | |||
+ | ===== Notes ===== | ||
+ | * A single // | ||
+ | * Input providers only provide input; they do not set it. Vehicles acquire input from input providers if '' | ||
+ | * All // | ||
+ | * '' | ||
+ | * Input is stored inside the '' | ||
+ | * To manually set the '' | ||
+ | |||
+ | ===== Available Bindings ===== | ||
+ | |||
+ | ==== Vehicle Input Provider Bindings ==== | ||
+ | <wrap round important> | ||
+ | ^ Name ^ Type ^ Keyboard Defaults ^ Gamepad Defaults ^ Description ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | **Module Bindings** ||||| | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ==== Scene Input Provider Bindings ==== | ||
+ | ^ Name ^ Type ^ Keyboard Defaults ^ Gamepad Defaults ^ Description ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | |||
+ | ===== Input Methods ===== | ||
+ | |||
+ | ==== Input Manager (Old/ | ||
+ | * Type of '' | ||
+ | * Uses old Unity InputManager. For new projects, it is recommended to use the Unity' | ||
+ | \\ | ||
+ | <wrap round info> | ||
+ | \\ | ||
+ | |||
+ | === Installation === | ||
+ | When first importing NWH Vehicle Physics 2 the project will be missing required bindings. There are two ways to add those: | ||
+ | - Manually adding each entry to the //Project Settings => Input// following the input bindings table available [[NWH: | ||
+ | - Copying the contents of // | ||
+ | * Close Unity. | ||
+ | * Open // | ||
+ | * Copy the contents of the provided InputBindings.txt file (//Scripts => Vehicle => Input => InputProviders => InputManagerProvider => InputBindings.txt// | ||
+ | * Open Unity. Check //Project Settings => Input//. The input bindings for NWH Vehicle Physics will appear towards the bottom of the list. | ||
+ | |||
+ | === Scene Setup === | ||
+ | To set up InputManager-based input in the scene add the following components to the scene: | ||
+ | - ' | ||
+ | - ' | ||
+ | Any vehicle that is present in the scene will now receive input from these providers. | ||
+ | |||
+ | ==== Input System (new) ==== | ||
+ | [{{ : | ||
+ | |||
+ | * 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. | ||
+ | |||
+ | <wrap round important> | ||
+ | |||
+ | === Installation === | ||
+ | |||
+ | * Install 'Input System' | ||
+ | * Under Edit => Project Settings => Player => Other Settings => '' | ||
+ | |||
+ | === Scene Setup === | ||
+ | * Add '' | ||
+ | * Default bindings can be modified by double clicking on '' | ||
+ | |||
+ | ==== Rewired Input Provider ==== | ||
+ | Since v1.7.1 NWH Vehicle Physics 2 also supports Rewired input. | ||
+ | |||
+ | === Installation === | ||
+ | * Download and import [[https:// | ||
+ | * Follow the Rewired install wizard. Make sure to leave both InputSystem and InputManager enabled. To use only InputManager, | ||
+ | * With the Rewired imported and installed, double click on //NWH => Vehicle Physics 2 => Scripts => Optional Packages => Input => Rewired// to import files needed for Rewired/ | ||
+ | * Open the newly imported Rewired folder and add '' | ||
+ | * Remove any existing '' | ||
+ | |||
+ | ==== Mobile Input Provider ==== | ||
+ | * Add '' | ||
+ | * Create a few UI => Button objects inside your canvas. Make sure that they are clickable. | ||
+ | * Remove the '' | ||
+ | * Drag the buttons to the corresponding fields in the '' | ||
+ | |||
+ | ==== Steering Wheel Input Provider ==== | ||
+ | For more info visit [[NWH: | ||
+ | |||
+ | ===== Scripting ===== | ||
+ | ==== Retrieving Input ==== | ||
+ | Since v1.0 multiple '' | ||
+ | < | ||
+ | float throttle = InputProvider.CombinedInput(i => i.Throttle()); | ||
+ | bool engineStartStop = InputProvider.CombinedInput(i => i.EngineStartStop()); | ||
+ | </ | ||
+ | \\ | ||
+ | Or to get the input from individual '' | ||
+ | < | ||
+ | float throttle = InputProvider.Instances[0].Throttle; | ||
+ | </ | ||
+ | \\ | ||
+ | |||
+ | ==== Manually Setting Input ==== | ||
+ | Input in each vehicle is stored in '' | ||
+ | < | ||
+ | In case input should not be retrieved from user but from another script - as is the case when AI is used - '' | ||
+ | Input now can be set from any script: | ||
+ | < | ||
+ | < | ||
+ | \\ | ||
+ | When using input generated by code (i.e. AI) it is usually handy to have access to a single axis throttle/ | ||
+ | < | ||
+ | vehicleController.input.autoSetInput = false; // Tells the vehicle to stop retrieving input values automatically. | ||
+ | vehicleController.input.Vertical = 0.5f; //Sets throttle to 0.5f, resets brakes. | ||
+ | vehicleController.input.Vertical = -0.5f; //Sets brakes to 0.5f, resets throttle. | ||
+ | </ | ||
+ | \\ | ||
+ | <wrap round important>'' | ||
+ | \\ | ||
+ | \\ | ||
+ | The alternative to this is using the '' | ||
+ | < | ||
+ | myVehicleController.input.inputModifyCallback.AddListener(MyInputModificationFunction); | ||
+ | --- | ||
+ | private void MyInputModificationFunction() | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | ==== Custom InputProvider ==== | ||
+ | If a custom '' | ||
+ | \\ | ||
+ | \\ | ||
+ | == Steps to create a new '' | ||
+ | * Create a new class, e.g. '' | ||
+ | < | ||
+ | * Override the methods that you want to use, e.g. '' | ||
+ | * The required methods are // | ||
+ | * Methods that are not used should return '' | ||
+ | * The newly created '' | ||
+ | \\ | ||
+ | Example custom input script is below. Note that to reference NWH.Common.Input the script will either need to be generated inside Scripts > Input folder of the asset or referenced inside the project .asmdef file if the script is placed outside of the VehiclePhysics directory. For more info about assembly definitions check out the [[: | ||
+ | |||
+ | == Example Custom VehicleInputProvider == | ||
+ | |||
+ | < | ||
+ | using NWH.Common.Input; | ||
+ | using UnityEngine; | ||
+ | using UnityEngine.InputSystem; | ||
+ | |||
+ | /// < | ||
+ | /// | ||
+ | /// </ | ||
+ | public class CustomVehicleInputProvider : VehicleInputProviderBase | ||
+ | { | ||
+ | public override void Awake() | ||
+ | { | ||
+ | base.Awake(); | ||
+ | // Your initialization code here (if needed). | ||
+ | } | ||
+ | |||
+ | public void Update() | ||
+ | { | ||
+ | // Your Update() code here (if needed). Just a standard MonoBehaviour Update(). | ||
+ | } | ||
+ | |||
+ | public override Throttle() | ||
+ | { | ||
+ | // Return your custom value here, example: | ||
+ | return 0.5f; // Replace this line with e.g. player.GetAxis(" | ||
+ | } | ||
+ | |||
+ | public override Steering() | ||
+ | { | ||
+ | // Return your custom steering value here. | ||
+ | return 0.123f; | ||
+ | } | ||
+ | |||
+ | // ...and so on. Override the functions that you want to use. If you do not need Clutch() for example, | ||
+ | // do not override it. | ||
+ | } | ||
+ | </ |