LoginSignup
0
0

More than 3 years have passed since last update.

Cybershoesを使ってUnityで歩く方法

Last updated at Posted at 2020-12-30

はじめに

Cybershoesを使ってUnityで歩く方法です。
「Unity Cybershoes」でググると某サンプルプロジェクトが出てくるのですが、不要な物が多いプロジェクトなので、なるべく最小構成での実装方法をまとめました。

開発環境

Windows10
Unity2019.4.11f1
Cybershoes
SteamVR Plugin 2.6.1
Oculus Rift(CV1)

SteamVR Pluginをインポート

基本的には公式サイトのクイックスタートに従って進めていきます。
https://valvesoftware.github.io/steamvr_unity_plugin/articles/Quickstart.html

アセットストアからSteamVR Pluginをインポートします。
すると以下のようなダイアログが表示されるので「Accept All」を選択します。
steamvr_dialog1.png

you made the right choice.png
「OK」で閉じます。

Unityのメニューから「Window」→「SteamVR Input」を選択します。
CopyExamples.png
上記のようなダイアログが表示されるので「Yes」を選択します。
MoveとJumpを追加.png
「Actions」の下にある「+」ボタンを2回押し、1つはNameを「Move」、Typeを「vector2」で追加し、2つめはNameを「Jump」、Typeを「boolean」で追加し、「Save and generate」を押します。

Assets/SteamVR/InteractionSystem/Samples/Interactions_Example
というサンプルシーンがあるので、再生して正常に動くか確認をします。
正常に動いていればこれでSteamVRのインポートは完了です。

バインディングの設定

Unityのメニューから「Window」→「SteamVR Input」を選択します。
「Open binding UI」を押し、コントローラのバインドウィンドウを開き、「バインドの新規作成」を押します。
コントローラのバインド.png

「default」のタブが選択されている事を確認し、「トラックパッドとして使用」の「位置」に「move」を設定し、Jumpの個所に「jump」を設定し、「個人用バインドを保存」を押して保存してからウィンドウを閉じます。
バインドにmoveとjumpを.png

CyberShoesの移動処理

新規でシーンを作成し、適当な3Dオブジェクトで足場を作成します。
プレイヤーには以下のような構成でCapsuleにカメラとスクリプトをアタッチします。
PlayerMovementSimpleプレハブの設定.png

PlayerMovementSimpleクラスは以下のような内容になっています。
接地しているかどうかの判定を省略しているため、空中でもジャンプができるようになっています。

PlayerMovementSimple.cs
using UnityEngine;
using Valve.VR;
public class PlayerMovementSimple : MonoBehaviour
{
    [SerializeField] float moveSpeed;
    [SerializeField] float jumpForce;
    [SerializeField] Transform hmdCamera;
    [SerializeField] SteamVR_Action_Vector2 moveAction;
    [SerializeField] SteamVR_Action_Boolean jumpAction;
    private Rigidbody rb;

    void Start()
    {
        SteamVR.Initialize();
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        if (jumpAction.GetState(SteamVR_Input_Sources.Treadmill))
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }

    void FixedUpdate()
    {
        float forwardInput = moveAction.GetAxis(SteamVR_Input_Sources.Treadmill).y;
        float sidewaysInput = moveAction.GetAxis(SteamVR_Input_Sources.Treadmill).x;
        Vector3 hmdDirectionForward = hmdCamera.forward - new Vector3(0, hmdCamera.forward.y, 0);
        Vector3 hmdDirectionRight = hmdCamera.right - new Vector3(0, hmdCamera.right.y, 0);
        Vector3 newVelocity = hmdDirectionForward * forwardInput + hmdDirectionRight * sidewaysInput;
        newVelocity *= moveSpeed;
        newVelocity.y = rb.velocity.y;
        rb.velocity = newVelocity;
    }
}

補足

原因は不明ですが設定が正しいプロジェクトでもCyberShoesが反応しない事が何度もありました。
その場合はSteamVRを再起動すると正常に動作するようになりました。
動作確認用にCyberShoesが動くことが確認できるプロジェクトを用意しておくと、プロジェクト依存なのか、ハードウェア依存なのかの原因の切り分けができて楽です。

UnityのTreadmillやCybershoes関連の情報があまりなかったため、情報が間違っていたらコメントなどでご指摘いただけるとありがたいです。
調べると情報はあったのですが、引用していいのか迷うソース(CyberRunnnerのプロジェクト丸ごと)だったので、引用はしませんでしたが参考にさせていただきました。

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