使用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
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章