1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Unity】InputSystemで1つのキーボードで2人分の操作を実装する方法

Last updated at Posted at 2023-11-29

■はじめに

UnityでInputSystemを利用して、キーボードをWASDと矢印キーで2人分使いたいなーと思いましたが、情報が少なかったので提供します

●結果

こんな感じで動かせます
InputTest.gif

■方法

●InputAction

  1. InputActionのAllControllSchemeってとこをクリック
    image.png

  2. こんなのが出てくるので、図の[KeyboardLeft]と[KeyboardRight]のように、キーボード用のスキームを2つ作る
    image.png

  3. 作ったスキーム用のActionをそれぞれ作る
    image.png

  4. 作ったBindingにスキームを割り当てる
    image.png

●コード

  • PlayerInputコンポーネントをつけたプレハブを PlayerInput.Instantiate()で生成する
    • 生成時に先ほど作ったスキームを割り当てる
InputManager.cs
    PlayerInput JoinProcess(string schemeName)
    {
        // 生成 (戻り値:PlayerInput)
        var player = PlayerInput.Instantiate(
            playerPrefab.gameObject, controlScheme: schemeName, pairWithDevice: Keyboard.current);
        
        player.defaultControlScheme = schemeName;
        player.gameObject.transform.SetParent(playerParent);
        
        return player;
    }

■動作確認

Window/Analysis/InputDebuggerから、ユーザーに割り当てられたキーを確認できる

image.png

■さいごに

マルチプレイゲームをデバッグ時に1人でテストプレイするときに便利~!

●参考

PlayerInput.Instantiate() / 公式リファレンス

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?