using UnityEngine;
using System.Collections;
using VRTK;
public class Y_VRTK_Helper : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public enum Devices
{
Headset,
Left_Controller,
Right_Controller,
}
public enum ControllerHand
{
Left,
Right
}
#region 手柄相关按键
///
/// 是否有手柄的按键按下
///
///
public static bool IsHandPressed()
{
if (IsRightHandPressed() || IsLeftHandPressed())
{
return true;
}
return false;
}
///
/// 右手柄是否有按键按下
///
///
public static bool IsRightHandPressed()
{
uint controllerIndex = GetRightHandIndex();
if (controllerIndex >= uint.MaxValue)
{
return false;
}
if (VRTK_SDK_Bridge.IsTriggerPressedOnIndex(controllerIndex) || VRTK_SDK_Bridge.IsGripPressedOnIndex(controllerIndex) || VRTK_SDK_Bridge.IsTouchpadPressedOnIndex(controllerIndex))
{
return true;
}
return false;
}
///
/// 获取右手柄的设备编号
///
///
public static uint GetRightHandIndex()
{
GameObject righthand = VRTK_DeviceFinder.GetControllerRightHand();
uint controllerIndex = VRTK_DeviceFinder.GetControllerIndex(righthand);
return controllerIndex;
}
///
/// 左手柄是否有按键按下
///
///
public static bool IsLeftHandPressed()
{
uint controllerIndex = GetLeftHandIndex();
if (controllerIndex >= uint.MaxValue)
{
return false;
}
if (VRTK_SDK_Bridge.IsTriggerPressedOnIndex(controllerIndex) || VRTK_SDK_Bridge.IsGripPressedOnIndex(controllerIndex) || VRTK_SDK_Bridge.IsTouchpadPressedOnIndex(controllerIndex))
{
return true;
}
return false;
}
///
/// 获取左手柄的设备编号
///
///
public static uint GetLeftHandIndex()
{
GameObject lefthand = VRTK_DeviceFinder.GetControllerLeftHand();
uint controllerIndex = VRTK_DeviceFinder.GetControllerIndex(lefthand);
return controllerIndex;
}
#endregion
#region VR的传输跳转
///
/// VR的传输跳转
/// 原本的跳转是通过手柄打射线到地面(或其他物体),跳转
/// 注意:[[CameraRig]]上要挂VRTK_BasicTeleport脚本
///
/// 这里的target就好比地面
/// 这里的pos(位置)就好比射线与地面碰撞的位置
/// 随便那个手柄,只要手柄上挂了VRTK_SimplePointer脚本就行
public static bool TelePort(Transform target,Vector3 pos, ControllerHand hand)
{
//VRTK_SimplePointer simplePointer;
GameObject controllerObj;
VRTK_DestinationMarker simplePointer;//VRTK_SimplePointer继承VRTK_DestinationMarker
if (hand == ControllerHand.Right)
{
controllerObj = VRTK_DeviceFinder.GetControllerRightHand();
simplePointer = controllerObj.GetComponent
}
else
{
controllerObj = VRTK_DeviceFinder.GetControllerLeftHand();
simplePointer = controllerObj.GetComponent
}
Transform headset = VRTK_DeviceFinder.DeviceTransform(VRTK_DeviceFinder.Devices.Headset);
print(string.Format("111,x:{0},y:{1},z:{2}", pos.x, pos.y, pos.z));
print(string.Format("headset,x:{0},y:{1},z:{2}", headset.localPosition.x, headset.localPosition.y, headset.localPosition.z));
//pos = new Vector3(pos.x - headset.localPosition.x, pos.y - headset.localPosition.y, pos.z - headset.localPosition.z);
//print(string.Format("222,x:{0},y:{1},z:{2}", pos.x, pos.y, pos.z));
if (simplePointer == null) return false;
uint controllerIndex = VRTK_DeviceFinder.GetControllerIndex(controllerObj);
DestinationMarkerEventArgs ars = new DestinationMarkerEventArgs();
ars.distance = Vector3.Distance(controllerObj.transform.position, pos);
ars.target = target;
ars.destinationPosition = pos;
ars.controllerIndex = controllerIndex;
ars.enableTeleport = true;
simplePointer.OnDestinationMarkerSet(ars);
return true;
}
///
/// VR的传输跳转
/// 原本的跳转是通过手柄打射线到地面(或其他物体),跳转
/// 注意:[[CameraRig]]上要挂VRTK_BasicTeleport脚本
///
/// 这里的target就好比地面(实验发现这个是什么好像没多大关系,最好还是用地面)
/// 这里的pos(位置)就好比射线与地面碰撞的位置
/// 头部视角所看的角度
/// 随便那个手柄,只要手柄上挂了VRTK_SimplePointer脚本就行
public static void TelePort(Transform target, Vector3 pos, float worldAngle, ControllerHand hand)
{
SetHeadSetViewAngle(worldAngle);
TelePort(target, pos, hand);
}
///
/// 设置头盔视角方向,在世界左边系下所对应的角度
///
///
public static void SetHeadSetViewAngle(float worldAngle)
{
Transform headset = VRTK_DeviceFinder.DeviceTransform(VRTK_DeviceFinder.Devices.Headset);
headset.parent.eulerAngles = new Vector3(0, worldAngle - headset.localEulerAngles.y, 0);//还要计算相对头盔的相对角
}
#endregion
}