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.

Joystick の操作を逆にしたい ~OnScreenStick に加工した値を流す~

Posted at

概要

以下の記事で Joystick の値を On-Screen Stick を使って Gamepad に流したが、Joystick の操作を逆にしたかったので、流す値を加工した。

横方向

Before After
before1.gif after1.gif

縦方向

Before After
before2.gif adter2.gif

方法

以下のようなスクリプトを用意する。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.OnScreen;

[RequireComponent(typeof(FloatingJoystick))]
public class OnScreenReverseStick : OnScreenControl
{
    private FloatingJoystick _joystick;

    void Start()
    {
        _joystick = GetComponent<FloatingJoystick>();
    }

    void Update()
    {
        // ここで入力値を逆にする
        var value = new Vector2(- _joystick.Horizontal, - _joystick.Vertical);
        SendValueToControl(value);
    }

    [InputControl(layout = "Stick"), SerializeField]
    private string _controlPath;

    protected override string controlPathInternal
    {
        get => _controlPath;
        set => _controlPath = value;
    }
}

Joystick から On-Screen Stick コンポーネントを外し、作成したスクリプトをアタッチする。
Screen Shot 2022-07-31 at 16.13.33.png

すると、概要の画像のように Joystick による回転が逆になる。

参考

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?