Unityに読み込んだキャラモデルをSteamVRでいやらしく眺めるためにGripMoveっぽい事ができるスクリプトを結構前に作った。
しばらくしてからもっといいのがないか探したけど上手く見つからないので、前のをなくさないようメモしておく。
Viveしか持ってないのでそれのみ動作確認。
SteamVR Pluginのバージョンは1.2.2。
使い方
コントローラにスクリプトをアタッチし、InspectorからObjCameraRig変数にCameraRigかそれの入っているオブジェクトを指定する。
このサンプルではViveコントローラのグリップボタン(にぎりの所のボタン)で移動する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR;
public class GripMove : MonoBehaviour {
// CameraRigもしくはそれを入れたオブジェクトを指定する
public GameObject ObjCameraRig;
//1フレーム前と差を比較するために前回のコントローラ位置・回転を格納する変数
private Vector3 beforeControllerPosition;
private Quaternion beforeControllerRotation;
void Update () {
SteamVR_TrackedObject trackedObject = GetComponent<SteamVR_TrackedObject>();
var device = SteamVR_Controller.Input((int)trackedObject.index);
//現在(移動後)のコントローラの位置・回転を格納
Vector3 afterControllerPosition = this.gameObject.transform.position;
Quaternion afterControllerRotation = this.gameObject.transform.rotation;
//"Grip"Moveなのでグリップボタンを入力したときに移動処理を実行
if (device.GetPress(SteamVR_Controller.ButtonMask.Grip))
{
//1フレーム前と比較したコントローラの移動距離を算出
Vector3 distanceDifference = beforeControllerPosition - afterControllerPosition;
//1フレーム前と比較したコントローラのY軸回転を算出
float rotationDifferenceY = Mathf.DeltaAngle(beforeControllerRotation.eulerAngles.y, afterControllerRotation.eulerAngles.y);
//コントローラが移動した距離分、CameraRigを移動
ObjCameraRig.transform.position += distanceDifference;
//コントローラのY軸が回転した分、コントローラ位置を軸としてCameraRigを回転移動
ObjCameraRig.transform.RotateAround(afterControllerPosition, Vector3.down, rotationDifferenceY);
}
//現在のコントローラの位置・回転を次回に移動前として使うために格納
beforeControllerPosition = afterControllerPosition;
beforeControllerRotation = afterControllerRotation;
}
}
ワールド座標でルームスケールする位置を移動するので、部屋ごと移動してるような不思議感覚。
コメント