LeftHand.csのコードにOnTriggerStay関数を追加する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class LeftHand : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
private GameObject pickedObject;
private bool moving = false;
private Vector3 dPosition;
private Quaternion dRotation;
// Update is called once per frame
void Update()
{
//Update Controller Position and Rotation
List<XRNodeState> nodes = new List<XRNodeState>();
InputTracking.GetNodeStates(nodes);
XRNodeState node = nodes[(int)XRNode.LeftHand];
node.TryGetPosition(out Vector3 position);
node.TryGetRotation(out Quaternion rotation);
transform.localPosition = position;
transform.localRotation = rotation;
if (moving) //in moving
{
//Update pickedObject Position and Rotation
pickedObject.transform.localPosition = transform.position + dPosition;
pickedObject.transform.localRotation = transform.rotation * dRotation;
if (Input.GetKeyUp("joystick button 14")) //releasing
{
moving = false;
pickedObject.GetComponent<Renderer>().material.color = Color.white;
}
}
}
void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown("joystick button 14")) //picking
{
pickedObject = other.gameObject;
dPosition = pickedObject.transform.position - transform.position;
dRotation = pickedObject.transform.rotation * Quaternion.Inverse(transform.rotation);
//Moving switch on
moving = true;
pickedObject.GetComponent<Renderer>().material.color = Color.red;
}
}
}
Windows MR によるオブジェクト操作
- Edit/ProjectSettings/XR Plugin Managementでインストールボタンを押す。
- HMDがWindows MRの場合はWindows Mixed Reality を選択。viveの場合はOpenVR XR Pluginをダウンロードして、ダブルクリック、OpenVR Loaderが追加されるので、これを選択。
- GameObject/XR/Convert Main Camera to XR Rigを実行するとMainCameraがXRRig/Camera Offsetの下に移動する。
- Main Camera の Clipping Planes/Nearを0.1ぐらいに変更
- XRRig/Camera Offset/MainCameraのTargetEyeをBothに変更
- XRRigのPositionを(0,0.3,-0.3)などにする。
- Right Hand(空のオブジェクト)を生成し、子に右手のモデル(Cubeなど)を配置
- 右手のモデルにTracked Pose Driver,Rigidbody,Manipulate.cs(下記)をアタッチ、コライダーのIs Triggerにチェック。
- Tracked Pose Driverで、Device:Generic XR Controller Pose Source:Right Controller
- 操作対象物としてHirerarchy/Create/3D Object/Cubeを原点に生成。大きさが一辺10cmの立方体となるようにScaleを(0.1, 0.1, 0.1)にする。
- ここで再生してみる。
Manipulate.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manipulate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
private GameObject pickedObject;
private bool moving = false;
private Vector3 dPosition;
private Quaternion dRotation;
// Update is called once per frame
void Update()
{
if (moving) //in moving
{
pickedObject.transform.position = transform.position + dPosition; //global
pickedObject.transform.rotation = transform.rotation * dRotation; //global
if (Input.GetKeyUp("joystick button 15")) //release
{
moving = false;
}
}
}
void OnTriggerStay(Collider other)
{
//Debug.Log("collid\n");
if (Input.GetKeyDown("joystick button 15")) //pick or moving 右手トリガーボタン15、左手14
{
Debug.Log("moving\n");
pickedObject = other.gameObject;
dPosition = pickedObject.transform.position - transform.position;
dRotation = pickedObject.transform.rotation * Quaternion.Inverse(transform.rotation);
//Moving switch on
moving = true;
//pickedObject.GetComponent().material.color = Color.red;
}
}
}
注意事項
- コントローラーの位置がおかしくなったらSteamVR,Windows MR を再起動してみる。
- 外部のモデルを取り込んだ時は、InspectorのStaticにチェックが入っている場合があり、チェックを外さないと動かない。