关于unity中使用 VRTK开发时,射线检测UI会返回后者,穿透前者的问题
一、修改 VRTK_UIGraphicRaycaster
在VRTK_UIGraphicRaycaster脚本第 59 行,找到如下一行:
eventData.pointerCurrentRaycast = nearestRaycast.Value;
将本行注释掉:
注释的目的是让射线投射从发射点开始计算,这行代码是检测到射线发射后最远处的UI
二、在VRTK_VRInputModule脚本的第 54 行,eventSystem.RaycastAll(pointer.pointerEventData, raycasts);的后面,补充代码:
raycasts.Sort((res1, res2) =>{
if (Mathf.Abs(res1.distance - res2.distance) < 0.001)
{
return res2.depth.CompareTo(res1.depth);
}
return res1.distance.CompareTo(res2.distance);
});
if (raycasts.Count > 0)
{
pointer.pointerEventData.pointerCurrentRaycast = raycasts[0];
}
这样,可以获得当前的射线最近的投射目标,因为最近的一个是列表中的第一个。
该修复方法来自于 GitHub finartist。
[代码]【GitHub】finartist issues/1846