1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unityでマウス・タッチ操作を共通化する:ドラッグ&ドロップ型ゲームの入力設計

1
Posted at

はじめに

PCではマウス、スマートフォンではタッチでオブジェクトをドラッグしたい。

この処理を各画面に直接書くと、入力判定・座標変換・ゲームルールが混ざり、端末ごとの差を直すたびに複数のスクリプトを修正することになります。

本記事ではUnity Input Systemを使い、入力を小さなクラスへ集約して、ドラッグ処理から分離します。

入力状態を1か所にまとめる

まず、現在のポインター位置と押下状態を返すクラスを作ります。

PointerInput.cs
using UnityEngine;
using UnityEngine.InputSystem;

public static class PointerInput
{
    private static bool TouchActive =>
        Touchscreen.current != null &&
        Touchscreen.current.primaryTouch.press.isPressed;

    public static Vector2 Position
    {
        get
        {
            if (TouchActive)
                return Touchscreen.current.primaryTouch.position.ReadValue();

            return Mouse.current?.position.ReadValue() ?? Vector2.zero;
        }
    }

    public static bool IsPressed =>
        TouchActive ||
        (Mouse.current?.leftButton.isPressed ?? false);

    public static bool WasPressedThisFrame =>
        (Touchscreen.current?.primaryTouch.press.wasPressedThisFrame ?? false) ||
        (Mouse.current?.leftButton.wasPressedThisFrame ?? false);

    public static bool WasReleasedThisFrame =>
        (Touchscreen.current?.primaryTouch.press.wasReleasedThisFrame ?? false) ||
        (Mouse.current?.leftButton.wasReleasedThisFrame ?? false);
}

UI側やゲーム側は、入力デバイスを意識せずPositionと押下状態だけを参照できます。

画面座標から対象を取得する

3Dオブジェクトをつかむ場合は、CameraからRayを飛ばします。

private bool TryPick(Vector2 screenPosition, out Draggable target)
{
    target = null;
    Ray ray = mainCamera.ScreenPointToRay(screenPosition);

    if (!Physics.Raycast(ray, out RaycastHit hit, 100f, draggableMask))
        return false;

    return hit.collider.TryGetComponent(out target);
}

draggableMaskを使い、床や装飾オブジェクトを誤って拾わないようにします。

ドラッグ状態を分ける

Update内で入力・移動・正解判定をすべて行うのではなく、開始・継続・終了の3状態に分けます。

private Draggable dragging;

private void Update()
{
    if (PointerInput.WasPressedThisFrame)
        BeginDrag(PointerInput.Position);

    if (dragging != null && PointerInput.IsPressed)
        ContinueDrag(PointerInput.Position);

    if (dragging != null && PointerInput.WasReleasedThisFrame)
        EndDrag();
}

この形にすると、EndDrag()でだけ装着可否を判定できます。毎フレーム正解判定を走らせる必要がありません。

ゲームルールを入力から分離する

PC組み立てゲームなら、ドロップ先は「CPUソケット」「メモリスロット」などです。入力処理側で部品名を比較するより、ドロップ先に受け入れ条件を持たせます。

public interface IDropTarget
{
    bool CanAccept(Draggable item);
    void Accept(Draggable item);
}

これにより、入力コードを変更せずに、新しい部品やステージを追加できます。

スマートフォンで追加したい調整

マウスとタッチは同じ入力ではありますが、操作感は同じではありません。実機では次も調整します。

  • 押した直後に動かさず、数ピクセルのドラッグ開始しきい値を設ける
  • 指で対象が隠れないよう、つかんだ部品を少し上へ表示する
  • 正しいドロップ先を色・輪郭・振動で伝える
  • 画面端ではカメラ移動とドラッグを競合させない
  • UI上のタッチをゲーム空間へ通さない

まとめ

入力を共通化する目的は、コードを短くすることだけではありません。

  • マウス/タッチの差分を1か所で吸収できる
  • ドラッグの状態遷移をテストしやすい
  • 部品追加時に入力処理を壊しにくい
  • WebGL・PC・スマートフォン間で操作感を調整しやすい

入力、座標変換、ドラッグ状態、ゲームルールを分離すると、「実際に物を持って組み立てる感覚」を段階的に改善できます。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?