HTC Vive 手柄发射射线功能

转载出处:http://www.manew.com/blog-7049-3768.html


先上截图,稍后补齐代码和详解。

[code]csharpcode:

/********************************************************************
 *
 *    Author:
 *      Data:
 *   Version:
 *  Function:
 *
 * *****************************************************************/
using UnityEngine;
using System.Collections;

public class RayTestCollder : MonoBehaviour
{
    [SerializeField]
    Transform rayPos;
    
    LineRenderer linerender;
	// Use this for initialization
	void Start ()
    {
        linerender = GetComponent();
	}
    RaycastHit hitInfo;
    // Update is called once per frame
    void Update ()
    {
        Ray ray = new Ray(rayPos.position,rayPos.forward);
        
        //Debug.DrawLine(ray.origin, hitInfo.point, Color.red, 2);//
        if (Physics.Raycast(ray,out hitInfo,1000))
        {
            GameObject gameobj = hitInfo.collider.gameObject;
            float distance = 0;
            if (gameobj != null)
            {
                distance = Vector3.Distance(transform.position, gameobj.transform.position);
            }
            else
            {
                distance = 1;
            }

            linerender.SetPosition(1, new Vector3(0, 0, distance));
        }
	}

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject != null)
        {
            //transform.gameObject.GetComponent().enabled = true;
            //transform.gameObject.GetComponent().enabled = true;

            //linerender.SetWidth(0, 1);
        }
    }
}

主要使用的组件和功能:LineRenderer+射线

个人感觉实现起来还算可以。

不足:射线顶端缺少标记点,想想后续会添加。也欢迎各位提出不同的实现方式互相学习!总体来说现在关于HTC开发这块的资料还是很少的。


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