HTC vive基于unity的凝视交互功能(带HTC 插件)

提供几个 HTC 插件 

1.SteamVR

2.VRTK - SteamVR Unity Toolkit

3.HTCUnity

链接:http://pan.baidu.com/s/1mhSt83Q 密码:0e2p


带上HTC眼镜后盯住物体几秒,然后弹出一个缩减时间的UI,在时间未结束以前移开视线清0

看物体触发事件的效果的原理其实就是从摄像机上发出一条射线,然后判断他碰到的是否是你想要触发的物体,接着发出对应事件即可


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class VRCameraRay : MonoBehaviour
{
    [SerializeField]
    private GameObject m_Canvas;
    [SerializeField]
    private float _myTime = 3;//时间总量
    [SerializeField]
    private GameObject m_cd;
    [SerializeField]
    private Image m_Image;
   
    void Update()
    {
        // 以摄像机所在位置为起点,创建一条向前发射的射线 
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            // 如果射线与平面碰撞,打印碰撞物体信息 
            //Debug.Log("碰撞对象: " + hit.collider.name);
            if (hit.collider.name == "Debris")
            {
                m_cd.gameObject.SetActive(true);
                CD();
                Countdown();
            }
            if (hit.collider.name != "Debris") {
                m_cd.gameObject.SetActive(false);
                _myTime = 3;
                m_Image.fillAmount = 1;
            }
            // 在场景视图中绘制射线 
            Debug.DrawLine(ray.origin, hit.point, Color.red);
        }
    }
   
    //====================倒计时=====================
    void Countdown()
    {
        _myTime -= Time.deltaTime;//时间总量-=完成一帧所用时间
        if (_myTime <= 0)
        {
            m_Canvas.gameObject.SetActive(true);
        }
    }
    //============技能冷却==============
    private bool m_isColding = true;
    void CD()
    {
        if (m_isColding)
        {
            m_Image.fillAmount -= (1.0f / _myTime) * Time.deltaTime;
            if (m_Image.fillAmount == 0)
            {
                m_cd.gameObject.SetActive(false);
            }
        }
    }
}



将本脚本挂在到  Camera (eye)  

m_Image




你可能感兴趣的:(Unity3D,HTC,Vive)