SteamVR2.0 でのコントローラを用いた移動・回転方法
後輩たちへのメモ程度にSteamVR2.0 での移動・回転方法を記述
1.下記のスクリプトを[CameraRig]
にアタッチ
2.InspectorのVR_cameraに[CameraRig]
の子オブジェクトCamera
を設定
3.ActionManifestで設定した項目を各SerializeField
に設定
※ここではActionManifestの説明を省くので,詳しくはググってください
気が向けば追記するかも
Unityで実行すると,左手で移動,右手で回転ができる
移動・回転スピードは値を変更してください
VR_Move.cs
using UnityEngine;
using Valve.VR;
public class VR_Move : MonoBehaviour
{
[SerializeField] SteamVR_Input_Sources HandRight;
[SerializeField] SteamVR_Input_Sources HandLeft;
[SerializeField] SteamVR_Action_Boolean up;
[SerializeField] SteamVR_Action_Boolean down;
[SerializeField] SteamVR_Action_Boolean right;
[SerializeField] SteamVR_Action_Boolean left;
[SerializeField] private float speed = 6.0f;
[SerializeField] private int Rotspeed = 2;
[SerializeField] private Camera VR_camera;
private float First_axis_Y = 0;
void Start ()
{
//Y軸の初期値を取得
First_axis_Y = transform.localPosition.y;
}
void Update ()
{
//左手トラックパッドで移動
if (up.GetState(HandLeft) == true)
{
transform.localPosition += VR_camera.transform.forward * speed * Time.deltaTime;
}
if (down.GetState(HandLeft) == true)
{
transform.localPosition -= VR_camera.transform.forward * speed * Time.deltaTime;
}
if (right.GetState(HandLeft) == true)
{
transform.localPosition += VR_camera.transform.right * speed * Time.deltaTime;
}
if (left.GetState(HandLeft) == true)
{
transform.localPosition -= VR_camera.transform.right * speed * Time.deltaTime;
}
//Y軸を初期位置に固定.空中に浮きたいor重力概念を追加する場合は以下をコメントアウトする.
transform.localPosition = new Vector3(transform.localPosition.x, First_axis_Y, transform.localPosition.z);
//右手トラックパッドで回転
if (right.GetState(HandRight) == true)
{
transform.Rotate(0, Rotspeed, 0);
}
if (left.GetState(HandRight) == true)
{
transform.Rotate(0, -Rotspeed, 0);
}
}
}