# Managing Inputs

{% hint style="info" %}
The asset utilizes the **Unity Input System** package, which means modifying inputs may differ from the old built-in Unity Input System. If you're unfamiliar with setting up the Input System package, please refer to the guide provided below.
{% endhint %}

{% content-ref url="managing-inputs/setting-up-input-system" %}
[setting-up-input-system](https://docs.twgamesdev.com/uhfps/guides/managing-inputs/setting-up-input-system)
{% endcontent-ref %}

1. Locate the **PlayerControls** asset within the **UHFPS -> Scriptables -> Input** folder. To open the **Input Actions** window, double-click on the asset.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FsrhT6RhJIhMFt5bchjlk%2FScreenshot_5.png?alt=media&#x26;token=1aa9500d-20aa-41a8-b3ef-2f387fb7614d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FmJKvhkvMYj98hsZP1UHb%2FScreenshot_6.png?alt=media&#x26;token=43eee9fe-760a-4641-9ac6-161e90bdea23" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
After opening the **Input Actions** window, you may notice that the action names follow the **input.action.something** pattern. This was in fact used as localization keys in previous versions of UHFPS. In the latest version of the asset this has been replaced with a better solution, so now it's up to you to name the inputs to your liking.
{% endhint %}

2. To create a new input action, simply click on the **(+)** icon found to the right of the **Actions** tab name. Enter the name of the action in the localization key pattern.

{% hint style="info" %}
Input names containing an **asterisk (\*)** will be automatically excluded from menu display and serialization. For instance, **input.action.look\*** will be disregarded.
{% endhint %}

3. Select the newly created action and modify the **Action Type** to the desired input type.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FuCCbiYlsxMVFEOq9zNlQ%2FScreenshot_7.png?alt=media&#x26;token=647ffa40-4dd5-4c50-8c69-4d6526cf511e" alt=""><figcaption></figcaption></figure>

4. Next, click on the **\<No Binding>** and choose the key you want your action to utilize.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FcdYUxaiLc0GyvhhLY8TL%2FScreenshot_8.png?alt=media&#x26;token=44f2028f-890d-472d-8908-b3a3c285fd18" alt=""><figcaption></figcaption></figure>

5. If you are using a custom controls asset, ensure that you assign the reference to this asset within the **Input Manager** component.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FZcRlfChPtZQI0Mca76kQ%2FScreenshot_9.png?alt=media&#x26;token=eef77c3f-6746-4753-bd9c-d24b7992b176" alt=""><figcaption></figcaption></figure>

6. In order to access input actions within a script, simply use the **Input Manager** functions. There are multiple ways to utilize the input.

{% tabs %}
{% tab title="In Update" %}

```csharp
using UnityEngine;
using UHFPS.Input;

private void Update()
{
    if (InputManager.ReadButtonToggle(this, Controls.CROUCH))
    {
        // do something when toggled on
        // even if you don't hold any button
    }

    if (InputManager.ReadButtonOnce(this, Controls.FIRE))
    {
        // do something once as button
    }

    if (InputManager.ReadButton(Controls.FIRE))
    {
        // do something every update as button
    }

    if(InputManager.ReadInput(Controls.LEAN, out float direction))
    {
        // do something every update with value
    }
}
```

{% endtab %}

{% tab title="Performed" %}

```csharp
using UnityEngine;
using UnityEngine.InputSystem;
using UHFPS.Input;

void Start()
{
    InputManager.Performed(Controls.FIRE, OnFire);
}

private void OnFire(InputAction.CallbackContext obj)
{
    // do something 
}
```

{% endtab %}

{% tab title="Observable" %}

```csharp
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UHFPS.Input;

IDisposable disposable;

void Start()
{
    disposable = InputManager.PerformedObservable(Controls.FIRE)
        .Subscribe(OnFire);
}

private void OnDestroy()
{
    // you have to dispose input event manually
    disposable.Dispose();
}

private void OnFire(InputAction.CallbackContext obj)
{
    // do something
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can find a script called **Controls**, which stores all the input names so that you don't have to keep typing out the whole input name over and over again.
{% endhint %}

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2F0gRN4aRSasYVLaTq5TlA%2FScreenshot_10.png?alt=media&#x26;token=4d2e019b-5eb2-455c-8397-119ddca2defe" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.twgamesdev.com/uhfps/guides/managing-inputs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
