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

相关推荐
anlog1 小时前
ini文件读取,EXE同目录文件读取
c#·ini
rockey6272 小时前
AScript之编译递归函数
c#·.net·script·动态脚本
yue0088 小时前
winform控件篇-按钮
c#
微三云生态系统架构师-彭丹11 小时前
任务卷轴积分系统架构设计:从任务状态机到合规风控的完整实现
unity·系统架构·游戏引擎
DevOpenClub12 小时前
Markdown、HTML 和 PPT 如何稳定交付:文档转换任务的幂等发布流程
开发语言·前端·c#·html·powerpoint
独立开发之道12 小时前
Three.js 创建 VR 内容:四步把一个普通 3D 场景送进头显
javascript·3d·vr
XR技术研习社13 小时前
从 PICO 文档看未来短期内的 Unity 版本选择
unity·游戏引擎·xr·vr
软件黑马王子13 小时前
3.单例模式问题1:构造函数
开发语言·单例模式·c#
软件黑马王子13 小时前
4.单例模式问题2:重复挂载
开发语言·单例模式·c#
夏霞15 小时前
c# 不支持的目标框架 解决方案
开发语言·c#