本篇为接上一篇对于HoloLens入门教程的深入研究
https://blog.csdn.net/gaofei12300/article/details/87712223
本篇主要是讲一下关于HoloLens视线触发,有点类似与制作cardboard小圆点的功能,可以用于触发一些事件的发生。
首先需要创建一个小圆点,可以创建一个球体,用于当作小圆点,缩小为0.1左右吧,根据自己的需要做调试。
下面贴出相关代码。将写好的脚本挂在你创建的球上即可。
有任何问题直接留言,看到会回复。 可以添加unity交流QQ群 207019099 备注“Hololens”。
public MeshRenderer meshRender;
public Camera mainCamera;
// Start is called before the first frame update
void Start()
{
meshRender = gameObject.GetComponentInChildren<MeshRenderer>();
mainCamera = Camera.main;
}
// Update is called once per frame
void Update()
{
Vector3 headPosition = mainCamera.transform.position;
//视角
Vector3 gazeDicection = mainCamera.transform.forward;
RaycastHit hit;
if (Physics.Raycast(headPosition, gazeDicection, out hit))
{
meshRender.enabled = true;
transform.position = hit.point;
transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
}
else
{
meshRender.enabled = false;
}
}