使用VRTK时的工具类,后续再增加

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 手柄相关按键

    /// <summary>
    /// 是否有手柄的按键按下
    /// </summary>
    /// <returns></returns>
    public static bool IsHandPressed()
    {
        if (IsRightHandPressed() || IsLeftHandPressed())
        {
            return true;
        }
        return false;
    }

    /// <summary>
    /// 右手柄是否有按键按下
    /// </summary>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// 获取右手柄的设备编号
    /// </summary>
    /// <returns></returns>
    public static uint GetRightHandIndex()
    {
        GameObject righthand = VRTK_DeviceFinder.GetControllerRightHand();
        uint controllerIndex = VRTK_DeviceFinder.GetControllerIndex(righthand);
        return controllerIndex;
    }

    /// <summary>
    /// 左手柄是否有按键按下
    /// </summary>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// 获取左手柄的设备编号
    /// </summary>
    /// <returns></returns>
    public static uint GetLeftHandIndex()
    {
        GameObject lefthand = VRTK_DeviceFinder.GetControllerLeftHand();
        uint controllerIndex = VRTK_DeviceFinder.GetControllerIndex(lefthand);
        return controllerIndex;
    }

    #endregion

    #region VR的传输跳转
    /// <summary>
    /// VR的传输跳转
    /// 原本的跳转是通过手柄打射线到地面(或其他物体),跳转
    /// 注意:[[CameraRig]]上要挂VRTK_BasicTeleport脚本
    /// </summary>
    /// <param name="target">这里的target就好比地面</param>
    /// <param name="pos">这里的pos(位置)就好比射线与地面碰撞的位置</param>
    /// <param name="hand">随便那个手柄,只要手柄上挂了VRTK_SimplePointer脚本就行</param>
    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<VRTK_DestinationMarker>();
        }
        else
        {
            controllerObj = VRTK_DeviceFinder.GetControllerLeftHand();
            simplePointer = controllerObj.GetComponent<VRTK_DestinationMarker>();
        }

        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;
    }

    /// <summary>
    /// VR的传输跳转
    /// 原本的跳转是通过手柄打射线到地面(或其他物体),跳转
    /// 注意:[[CameraRig]]上要挂VRTK_BasicTeleport脚本
    /// </summary>
    /// <param name="target">这里的target就好比地面(实验发现这个是什么好像没多大关系,最好还是用地面)</param>
    /// <param name="pos">这里的pos(位置)就好比射线与地面碰撞的位置</param>
    /// <param name="worldAngle">头部视角所看的角度</param>
    /// <param name="hand">随便那个手柄,只要手柄上挂了VRTK_SimplePointer脚本就行</param>
    public static void TelePort(Transform target, Vector3 pos, float worldAngle, ControllerHand hand)
    {
        SetHeadSetViewAngle(worldAngle);
        TelePort(target, pos, hand);

    }

    /// <summary>
    /// 设置头盔视角方向,在世界左边系下所对应的角度
    /// </summary>
    /// <param name="worldAngle"></param>
    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
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值