LoginSignup
5
10

More than 5 years have passed since last update.

OculusGoのコントローラ入力とキャラクター移動

Last updated at Posted at 2018-11-20

概要

OculusGoでのキャラクター移動関連についてまとめる。

初期設定

コントローラの入力について

入力の受け取りは下記の通り。

// 押下げた瞬間
OVRInput.GetDown

// 押上げた瞬間
OVRInput.GetUp

// 押している間
OVRInput.Get

キーコードは下記の通り。

// トリガー
OVRInput.Button.PrimaryIndexTrigger

// 戻るボタン
OVRInput.Button.Back

// タッチパッド
OVRInput.Button.PrimaryTouchpad

// タッチパッドに触れている
OVRInput.Touch.PrimaryTouchpad

// タッチされた座標 (Vector2)
OVRInput.Axis2D.PrimaryTouchpad

例えば、トリガーを押した瞬間に何かしたければ下記のようになる。

if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger)) {
    // 処理
}

移動の仕組みについて

タッチパッドの入力を検知したら、どの部分が押されたかを取得する。
その後、押された部分によって移動方向を確定する。

  1. タッチパッド入力検知
  2. タッチされた座標を取得
  3. 上下左右のどの部分が押されたかを判定
  4. 上下なら移動量、左右なら回転量を取得
  5. 取得した移動量と回転量を OVRCameraRig に追加する

入力検知は下記の通り。

if (OVRInput.Get(OVRInput.Button.PrimaryTouchpad) { }

タッチされた座標の取得は下記の通り。

Vector2 pt = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad);

上下左右のどの部分が押されたかの判定は下記の通り。

// 方向を enum で定義しておく
public enum ControllerDirection {
    Front,
    Right,
    Back,
    Left,
    None
}

// 移動座標を取得するメソッドを用意
private ControllerDirection FetchDirection(Vector2 pt) {
    if (pt.x < -0.5 && -0.5 < pt.y && pt.y < 0.5) {
        return ControllerDirection.Left;
    }
    if (pt.x > 0.5 && -0.5 < pt.y && pt.y < 0.5) {
        return ControllerDirection.Right;
    }
    if (pt.y < -0.5 && -0.5 < pt.x && pt.x < 0.5) {
        return ControllerDirection.Back;
    }
    if (pt.y > 0.3 && -0.5<pt.x && pt.x<0.5) {
        return ControllerDirection.Front;
    }

    return ControllerDirection.None;
}

移動量と回転量の取得は下記の通り。

// キャラの移動量を取得
public Vector3 FetchMoveVector(Vector2 pt) {
    switch (FetchDirection(pt)) {
        case ControllerDirection.Front:
            return new Vector3(0, 0, 0.5f * Time.deltaTime);
        case ControllerDirection.Back:
            return new Vector3(0, 0, -0.5f * Time.deltaTime);
        default:
            return new Vector3(0, 0, 0);
    }
}

// キャラの回転量を取得する
public float FetchMoveAngle(Vector2 pt) {
    switch (FetchDirection(pt)) {
        case ControllerDirection.Right:
            return 30f;
        case ControllerDirection.Left:
            return -30f;
        default:
            return 0f;
    }
}

取得した移動量と回転量の追加は下記の通り。

// 移動量の追加
Camera.transform.Translate(FetchMoveVector(pt));

// 回転量の追加
float angle = _characterMoveCalculator.FetchMoveAngle(pt);
Camera.transform.rotation *= Quaternion.Euler(0, angle, 0);

まとめ

移動と回転の仕方の正解はイマイチ分かっていない為、間違い等があれば指摘して頂けると幸いです。

5
10
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
5
10