0
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 DOTS】 RivalをInputSystemに対応させたい。

Last updated at Posted at 2023-02-21

お久しぶりです。

あけましておめでとうございます。雑記久々にやります。誤情報注意報

自己紹介

Unityを適当にたしなむ大学生です。

Rivalとは

Unity公式が配布してるUnity DOTS用キャラクターコントローラーです。なぜか旧Input Managerを使ってるのであれが嫌いな私はInput Systemに変えたいということです。
GitHubで配布されてます。
公式パッケージになりましたPackage Managerからcom.unity.charactercontrollerでインストールできます。(2023/4/17更新)

実際に使う

これ読めばいけます。

Input Systemを適用していく!!

環境

Unity 2022.2.1f1
Entites 1.0.0-pre.15
Input System 1.4.4
Rival 1.0.0-pre.15.1

準備

Input Systemの使い方は省略しますが、C#のスクリプトを生成する必要があるので、そこだけ紹介。
image.png
Input Action AssetのGenerate C# Classにチェック入れるだけです。

コードを書く!

Rivalのチュートリアルをやっていくとキャラクターを動かせる段階まできます。そうするとThirdPersonPlayerSystem.csもしくはFirstPersonPlayerSystem.csが生成されます。
その中身をちょろっと変えるだけです。

フィールド

    private GuardAndStealActions actions;
    private InputAction playerMovementAction;
    private InputAction playerLookAction;
    private InputAction playerJumpAction;

フィールドを作ります。GuardAndStealActionsというクラスは先ほど生成したInput Action Assetのクラスです。

OnCreate

    protected override void OnCreate()
    {
        //ここから改変ポイント
        //アクションの初期化
        actions = new();
        actions.Enable();
        playerMovementAction = actions.Player.Move;
        playerLookAction = actions.Player.Look;
        playerJumpAction = actions.Player.Jump;
        //ここまで

        RequireForUpdate<FixedTickSystem.Singleton>();
        RequireForUpdate(SystemAPI.QueryBuilder().WithAll<ThirdPersonPlayer, ThirdPersonPlayerInputs>().Build());
    }

ここはInputActionsの初期化を行うだけですね。

TIPS

2022.2からC#の新しいバージョンに対応して、インスタンス化の時に

クラス名 変数名 = new();

で型推論してくれて初期化できるようになってます。Dictionaryとかジェネリクス系を使うときはめちゃめちゃ便利なので使ってみてください

OnDestroy

    protected override void OnDestroy()
    {
        //アクションの停止
        actions.Disable();
        actions.Dispose();
    }

もともとOnDestroyは定義されてないのでここは普通に全部オリジナルです。

OnUpdate

    protected override void OnUpdate()
    {
        uint fixedTick = SystemAPI.GetSingleton<FixedTickSystem.Singleton>().Tick;

        foreach (var (playerInputs, player) in SystemAPI.Query<RefRW<ThirdPersonPlayerInputs>, ThirdPersonPlayer>())
        {
            //ここからオリジナル
            playerInputs.ValueRW.MoveInput = playerMovementAction.ReadValue<Vector2>();
            playerInputs.ValueRW.CameraLookInput = playerLookAction.ReadValue<Vector2>();
            playerInputs.ValueRW.CameraZoomInput = 0.0f;
            //ここまで
            
            // For button presses that need to be queried during fixed update, use the "FixedInputEvent" helper struct.
            // This is part of a strategy for proper handling of button press events that are consumed during the fixed update group
            //条件式だけオリジナル
            if (playerJumpAction.ReadValue<float>() > 0)
            {
                playerInputs.ValueRW.JumpPressed.Set(fixedTick);
            }
        }
    }

はい。以上です。いたって簡単。特に説明することもないですね。

感想

5分ぐらいでInputSystemに変更できました。Rivalが良くできている証拠だと思います。昨日Rivalを発見したばっかりなので全く触ってないですが、DOTSを使ったキャラクターコントローラーは凄く自作難易度が高いと思うのでこのライブラリ神です。
自作難易度高い件については前の記事を見てもらったらわかるかもしれません。
また、Rivalについては触れます。ではまたいつの日か…

0
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
0
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?