本篇爲接上一篇對於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;
}
}