# Input System Example

## 1. Install Input System Package

Go to Window -> Package Manager

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FFbBwk2tvzkS1PrVige5o%2Fimage.png?alt=media&#x26;token=94a437ff-be25-40f1-9717-6fd3edd24afc" alt="" width="270"><figcaption></figcaption></figure>

Search from the Unity Registry

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FAjEcDc7TN1lbtzgvGeFJ%2Fimage.png?alt=media&#x26;token=78659a37-a538-42eb-8ed2-e61511158ce4" alt="" width="264"><figcaption></figcaption></figure>

Type "Input System" in the search bar and install

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2F13z0e1ckufwAN9IrSvS4%2Fimage.png?alt=media&#x26;token=d9e95166-6054-4300-8b4c-837101543495" alt=""><figcaption></figcaption></figure>

## 2. Create Input Actions Asset

Create input actions asset by doing right-click  in the folder -> Create -> InputActions

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FMAuhkjotGg8OVQpALwmX%2Fimage.png?alt=media&#x26;token=f4f0c855-9431-4840-97d7-ead0f7b0b4e6" alt="" width="540"><figcaption></figcaption></figure>

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FPyTUucT0mxvSzg1eGXmj%2Fimage.png?alt=media&#x26;token=6256a638-a287-4431-9cfc-51b317711978" alt="" width="158"><figcaption></figcaption></figure>

## 3. Add Input Actions

Open the input actions asset and create new action map by clicking "+".

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2F30hoqI10YoROQeYFTzpt%2Fimage.png?alt=media&#x26;token=ee360100-4ec3-4933-a099-585a8edefdde" alt="" width="563"><figcaption></figcaption></figure>

Create new action called "GasInput" and set it's action type to "Value" and Control Type to "Any".

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FqgRWQO3SmzjwFywUIlg5%2Fimage.png?alt=media&#x26;token=aa2b3a36-91e1-494c-9580-806a87335fe0" alt="" width="563"><figcaption></figcaption></figure>

Add bindings by clicking "+". As a path select the input button

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2Fu9mjSvqFqRN8ScGhd9xD%2Fimage.png?alt=media&#x26;token=017b2192-5540-47fe-a30d-9fa17a9101b2" alt="" width="563"><figcaption></figcaption></figure>

Repeat the same steps for the Brake Input&#x20;

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FICPwLcFkfsCVVafI8ueg%2Fimage.png?alt=media&#x26;token=7203279c-c9e2-4538-b11e-8b8032ffe787" alt="" width="563"><figcaption></figcaption></figure>

Create new action map for the steering input. Add Positive/Negative binding for the WASD controls

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FNZv6s5GPlMGnKATwMNCB%2Fimage.png?alt=media&#x26;token=a0dc8791-bf9c-4f52-b511-1ff564289b85" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FwJWmy3pADXuZauBAeu5w%2Fimage.png?alt=media&#x26;token=f812e7b6-7405-4109-812c-4a132932b87b" alt="" width="563"><figcaption></figcaption></figure>

Add binding for the left stick horizontal input

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2F31hxezNw57REaTSjTH67%2Fimage.png?alt=media&#x26;token=be27f3db-1c38-40d6-a0f6-dbf8a48c911b" alt="" width="563"><figcaption></figcaption></figure>

Create Handbrake action of type "Button" and add input paths

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2F6nlWY4FpmomN3B4P21jX%2Fimage.png?alt=media&#x26;token=d2580f07-b11f-446b-8bb2-b42deb8381d4" alt="" width="563"><figcaption></figcaption></figure>

Save the asset and close the window.

## 4. Create a Script&#x20;

Click on the InputActions asset, check the "Generate C# Class" toggle and click "Apply"

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FLXws4Ov2Fsq6BiEV2Np6%2Fimage.png?alt=media&#x26;token=62f6b975-b1f8-47cd-b6ab-49a120bf792c" alt="" width="386"><figcaption></figcaption></figure>

Create new C# script

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FFDNjIiZzVLbVumENknHF%2Fimage.png?alt=media&#x26;token=886e68fe-5400-4b02-81e2-a0c38db02388" alt="" width="538"><figcaption></figcaption></figure>

Open the script. Add the namespace by typing "using Assets.VehicleController". Inherit from the IVehicleControllerInputProvider

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FJQvJW3qlAfdfRFj2Z13v%2Fimage.png?alt=media&#x26;token=e00e606f-58e4-48db-ac70-9a5b227bc071" alt="" width="548"><figcaption></figcaption></figure>

Create a new object of type PlayerInputActions (the generated class from the input actions) in Awake.

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FriKIyt0Zje0eukw03ySr%2Fimage.png?alt=media&#x26;token=0e31d7e1-c0af-4c02-bedf-49ba23785b7c" alt="" width="269"><figcaption></figcaption></figure>

Implement the interface

{% hint style="info" %}
In Visual Studio, click on the IVehicleControllerInput provider, which should have a red wavy underline, press Alt + Enter and select "Implement Interface"
{% endhint %}

<figure><img src="https://3780384024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVPyvySfaC85jUgVcDPRJ%2Fuploads%2FovsLugXs7I1IuUFkHHz3%2Fimage.png?alt=media&#x26;token=3cfa4086-05af-474e-a152-b2a1773cffc6" alt="" width="563"><figcaption></figcaption></figure>

{% hint style="info" %}
GearUp/GearDown should be actions of type button. In code, you can read the value by calling inputActions.Vehicle.GearUp.WasPerformedThisFrame().

NitroBoost should be implemented like Handbrake.

Pitch/Yaw/Roll should be implemented like HorizontalInput.
{% endhint %}
