离谱!怎么有两个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不一样。如果后面再出现这种问题,就需要检查一下设备有没有找对。

相关推荐
RobinDevNotes2 小时前
一套代码发布到十几个平台:Cocos入门速览
typescript·c#·游戏开发
阿松爱学习4 小时前
【Unity开发】监听Scene场景事件
unity·c#
xiaogai_gai12 小时前
BOM 物料清单同步实战:金蝶云星空 → 四化智造 MES 修改场景深度拆解
unity·游戏引擎
天天代码码天天13 小时前
屏幕翻拍满是彩色条纹?用 C# + Vulkan 做了一个去摩尔纹工具,支持单图与文件夹批量
c#·去摩尔纹
2501_9307077813 小时前
使用C#代码在 Word 文档中添加、插入或删除页面
开发语言·c#·word
淡海水15 小时前
03-03-架构篇-SGen GC架构解析
unity·架构·c#·gc·sgen
chaoyuanl15 小时前
从平面到包裹:超元力飞行影院如何构建“人在景中、身随景动”的感知闭环
人工智能·科技·平面·xr·vr·娱乐
chaoyuanl16 小时前
超元力VR大空间CS项目落地解决方案:五大选购要点,开店全程扶持
大数据·人工智能·vr
曹牧1 天前
C#:输入的字符串格式不正确
c#
萌萌的提莫队长1 天前
Unity相机捕捉,图像识别,抠像+合成
opencv·unity