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

相关推荐
格林威23 分钟前
工业相机图像高速存储(C#版):先存内存,后批量转存方法,附海康相机实战代码!
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·海康相机
sanshizhang39 分钟前
C#如何获取CAD的对象并修改
windows·c#·cad插件
小曹要微笑1 小时前
C#中什么是类
开发语言·c#·面向对象·
xhyyvr1 小时前
VR应急救护学习机|沉浸式体验VR应急救护
学习·vr
Singe.Chen1 小时前
C# 配置文件加密解密:最简便的实现方案
开发语言·网络协议·c#·信息与通信
bugcome_com2 小时前
C# 委托(Delegate)详解:从入门到高级用法
c#
星幻元宇VR2 小时前
VR生产安全学习机|开启智慧安全培训新时代
人工智能·科技·学习·安全·vr
星幻元宇VR2 小时前
VR科普学习一体机,在学校教育的应用前景
科技·学习·安全·vr·虚拟现实
猹叉叉(学习版)2 小时前
【ASP.NET CORE】 7. Identity标识框架
笔记·后端·c#·asp.net·.netcore
喜欢喝果茶.2 小时前
(c#)System.Windows -> SunnyUI
c#