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

相关推荐
初九之潜龙勿用1 天前
C# 操作Word模拟解析HTML标记之背景色
开发语言·c#·word·.net·office
时光追逐者1 天前
使用 MWGA 帮助 7 万行 Winforms 程序快速迁移到 WEB 前端
前端·c#·.net
老骥伏枥~1 天前
【C# 入门】程序结构与 Main 方法
开发语言·c#
全栈师1 天前
java和C#的基本语法区别
java·开发语言·c#
钰fly1 天前
联合编程(加载单个工具,ini读写,图片读写,setting存储)
c#
FuckPatience1 天前
C# 对象初始化器对属性赋值vs构造函数里对属性赋值
c#
m0_748233171 天前
C语言vsC#:核心差异全解析
c语言·开发语言·c#
淡海水1 天前
【节点】[Houndstooth节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·houndstooth
MyBFuture1 天前
C# 关于联合编程基础
开发语言·c#·visual studio·vision pro
Sunsets_Red1 天前
单调队列优化dp
c语言·c++·算法·c#·信息学竞赛