离谱!怎么有两个raycast方向 -- Unity for VR Controller Raycasting

csharp 复制代码
    public GameObject leftController;
    public GameObject rightController;

	
    public void OnOptionSelected(InputAction.CallbackContext context)
    {
        if (InteractionController.Instance.currentModeName != "SideBySideMode") { return; }

        if (context.action.name == "SelectOption")
        {
            if (context.performed)
            {
                GameObject controller = rightController;
                Ray ray = new Ray(controller.transform.position, controller.transform.forward);
                RaycastHit hit;
                int targetLayerMask = 1 << LayerMask.NameToLayer("SideBySideClickable");

                if (Physics.Raycast(ray, out hit, Mathf.Infinity, targetLayerMask))
                {
                    Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 10f);

                    if (hit.collider != null)
                    {
						//logic
                    }
                }
                else
                {
                    Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 10f)

                    Debug.Log("No hit in side by side mode");
                }
            }
        }
    }

上面的代码得到的raycast是下图红线,而实际上我的controllor指着的是绿线的方向。我凌乱了,怎么得到的raycast不一样。

找到的原因是我的hirarchy view里面,应该把RightController和LeftController拖进去,而我一开始拖动的是XRControllerRight和XRControllerLeft,因此会有上面的问题。

更深层的原因就是因为XRControllerRight和RightController的transform不一样。如果后面再出现这种问题,就需要检查一下设备有没有找对。

相关推荐
李高钢6 小时前
C# WinForms 架构与项目实战:多窗体通信、三层架构、数据库、日志与配置
架构·c#·winforms
zhchyun20087 小时前
【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(05)
ui·unity·游戏引擎
OPEN-F8 小时前
Python进阶教程:自动化办公实战
python·c#·自动化
旗开得胜马到成功9 小时前
工控系统备份的底层技术解析:老旧协议、零停机窗口与中科热备的专项破局
运维·服务器·vr
czhc11400756639 小时前
C# 处理体数据:装、钉、交、取消
c#
_ZHOURUI_H_10 小时前
不做完整 ECS,只优化数据布局:Unity EasyECS 到底是什么
unity·性能优化·游戏引擎
淡海水10 小时前
03-03-线性-LinkedList-T-源码不变式与选择边界
windows·链表·c#·编译·linkedlist·clr·机器码
CDN36010 小时前
360CDN发布元宇宙与VR/AR加速方案:边缘渲染与超低延迟流媒体,彻底终结眩晕感
ar·vr·cdn加速·cdn防御
longxiaozhang61 天前
C# Array类:数组其实没那么难
开发语言·c#
longxiaozhang61 天前
C#运算符重载:让对象也能使用“加减乘除”
开发语言·c#