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

相关推荐
沃码13 小时前
【Lively Wallpaper 】插件开发:LivelyProperties.json 全流程实战教程
c#·json·livelywallpaper·视频壁纸
淡海水17 小时前
【节点】[IrisOutOfBoundColorClamp节点]原理解析与实际应用
unity·游戏引擎·iris·shadergraph·图形·clamp·bound
github.com/starRTC18 小时前
Claude Code中英文系列教程34:再谈Skills
开发语言·c#·ai编程
bugcome_com20 小时前
C# 判断语句详解(if、switch、三目运算符、Null 条件运算符)
c#
霸王•吕布1 天前
C#-使用OpenTK渲染3D模型
c#·opentk·glcontrol
游乐码1 天前
c#封装成员变量和成员方法和访问修饰符
开发语言·c#
Never_Satisfied1 天前
在c#中,Jint的AsString()和ToString()的区别
服务器·开发语言·c#
Never_Satisfied1 天前
在c#中,获取文件的大小
java·开发语言·c#
weixin_468466851 天前
PyTorch导出ONNX格式分割模型及在C#中调用预测
人工智能·pytorch·深度学习·c#·跨平台·onnx·语义分割
上证50指数etf1 天前
Unity面试总结
unity