最近はまたUnityで2Dのゲームを作っています
いろいろとお試ししているのですが 従来の入力システムとは違うInputSystemというパッケージを使ってみたので そのやり方を忘れないうちに残しておきます
1.Unityエディタメニューの ウィンドウ/パッケージマネージャーを選択

2.パッケージをUnityレジストリに変更

3.Input Systemを選択してインストールをクリック

4.再起動するか確認されるのでYesをクリック(再起動するとInput Managerが無効化されInput Systemが有効になる)

5.プロジェクトタブ内で右クリックし作成/Input Actionを選択 名前をPlayerActionにする

6.PlayerActionダブルクリックしAction Mapを追加(+をクリックする) 名前はPlayerにする

7.Actionを追加し名前をMoveLeftにする Binding/Pathの▼をクリック

8.Keyboard/By Location of Key/Left Arrowを選択



9.同じようにActionを追加しMoveRight MoveUp MoveDownを設定してセーブ



10.Playerオブジェクトのコンポーネントを追加をクリックしPlayer Inputを追加

11.PlayerActionをドラッグ&ドロップ BehaviorをInvoke Unity Eventsに変更

12.プロジェクトタブ内で右クリックし作成/C#スクリプトを選択 名前をPlayerMoveにする

13.PlayerMoveスクリプトを編集
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMove : MonoBehaviour
{
Rigidbody2D rb;
[SerializeField] float speed = 1.5f;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
public void MoveLeft(InputAction.CallbackContext context)
{
if (context.started)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
else if (context.canceled)
{
rb.velocity = Vector2.zero;
}
else
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
}
public void MoveRight(InputAction.CallbackContext context)
{
if (context.started)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else if (context.canceled)
{
rb.velocity = Vector2.zero;
}
else
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
}
public void MoveUp(InputAction.CallbackContext context)
{
if (context.started)
{
rb.velocity = new Vector2(rb.velocity.x, speed);
}
else if (context.canceled)
{
rb.velocity = Vector2.zero;
}
else
{
rb.velocity = new Vector2(rb.velocity.x, speed);
}
}
public void MoveDown(InputAction.CallbackContext context)
{
if (context.started)
{
rb.velocity = new Vector2(rb.velocity.x, -speed);
}
else if (context.canceled)
{
rb.velocity = Vector2.zero;
}
else
{
rb.velocity = new Vector2(rb.velocity.x, -speed);
}
}
}
14.Playerオブジェクトのコンポーネントを追加をクリックしRigidbody 2Dを追加

15.Rigidbody 2Dのボディタイプをキネマティックにする
PlayerMoveスクリプトをPlayerオブジェクトにドラッグ&ドロップ

16.Player Inputのイベント/Playerの▼をクリック MoveLeftにPlayerオブジェクトをドラッグ&ドロップ
FunctionにPlayerMove/MoveLeftを設定する


17.同じようにMoveRightにPlayerMove/MoveRight MoveUpにPlayerMove/MoveUp MoveDownにPlayerMove/MoveDownを設定する

実行して矢印キーを押すと左右上下に移動します

スマートなやり方ではないですがとりあえず使ってみました
私が作る簡単なゲームの入力には従来の入力システムで十分な気がしますが お試しは楽しいです(笑)