# New Input System - 2D Move

## Creating Input Actions

Right click in the project folder and then go to "Create" -> Input Actions

<figure><img src="/files/Zti7CAGEGit0vczsFFJ5" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/XXBrcCADiiMsMAJtEtLe" alt=""><figcaption><p>First step is setting up an action and the "Control Type."</p></figcaption></figure>

<figure><img src="/files/Mze5CaJbw3pCYhMk8zkD" alt=""><figcaption><p>Then, when you add a binding to an action</p></figcaption></figure>

<figure><img src="/files/lXcQcbAb9bD2sn27CdOA" alt=""><figcaption><p>Click the "Apply" button.</p></figcaption></figure>

## Player GameObject

This has four components. I've opted to put the sprite rendered on a child game object of this because of a shielding function later.

The components needed here are:

* Collider2D - Anyone will do
* Rigidbody 2D
* Player Input
* Custom script to control the player gameobject, named here "Player Controller"

<figure><img src="/files/rQWvqHofclHJd2bKe1In" alt=""><figcaption></figcaption></figure>

Note: For the "Collision Detection" property, set it to "Continuous" or the camera gets shaky.

## Invoke Unity Events

Because we're going to be "Behavior: Invoke Unity Events," you <mark style="color:red;">**do not need**</mark> the following code.

This method is more performant than "Broadcast Messages" because this will only trigger once the input is interacted with rather than sending it every frame.

```
private void Awake() {
playerInput = GetComponent<PlayerInput>();
}

private void OnEnable() { //some code to activate the input actions }
private void OnDisable() { //some code to deactivate the input actions }
```

<figure><img src="/files/FjUDKONsPvV3hibKx955" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/GBtzmzaY8uyti7mCevNv" alt=""><figcaption></figcaption></figure>

PlayerController.cs script that is attached to the player GameObject in the scene.

Be sure to add "using UnityEngine.InputSystem;" The parameter "InputAction.CallbackContext ctx" makes this device specific. If you don't include it, it will read all the input from all devices for one character. "ctx" can be called anything but "InputAction.CallbackContext" has to remain the same.

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    private Vector2 movementInput;
    [SerializeField] private float MOVESPEED = 4.0f;
    private Rigidbody2D rb;


    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    public void OnMove(InputAction.CallbackContext ctx)
    {
        movementInput = ctx.ReadValue<Vector2>();
    }

    private void FixedUpdate()
    {
        //Moving the character
        rb.velocity = movementInput * MOVESPEED;
    }

}
```

Alternate way to move character.

<pre class="language-csharp"><code class="lang-csharp"><strong>//Alternate 1
</strong>rb.velocity = movementInput * (MOVESPEED * Time.fixedDeltaTime);

<strong>//Alternate 2
</strong><strong>rb.MovePosition(rb.position + movementInput * (MOVESPEED * Time.fixedDeltaTime));
</strong></code></pre>

```
```


---

# 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://roxioxx.gitbook.io/unity-cookbook/2d/new-input-system-2d-move.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.
