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

相关推荐
xiaoshuaishuai81 小时前
C# 实现不掉线的CRM
开发语言·c#
mxwin1 小时前
Unity Shader 预乘 Alpha 完全指南 解决半透明纹理边缘黑边问题,让你的 UI 渲染更干净
unity·游戏引擎
mxwin1 小时前
Unity URP 软粒子(Soft Particles)完全指南
unity·游戏引擎·shader
mxwin1 小时前
Unity Shader 深度偏移Depth Bias / Offset 完全指南
unity·游戏引擎·shader
ykjhr_3d2 小时前
广州哪里有vr电力行业事故体验系统
vr·虚拟现实·华锐视点
星幻元宇VR2 小时前
VR动感科普单车|让交通安全教育更有参与感
科技·学习·安全·vr·虚拟现实
星河耀银海2 小时前
Unity基础:UI组件详解:Button按钮的点击事件绑定
ui·unity·lucene
筱璦3 小时前
C#期货分仓、策略交易模拟演示系统(含资源下载)
开发语言·c#·策略模式·量化交易·期货交易
唐青枫3 小时前
C#.NET Task 与 async await 深入解析:底层原理、执行流程与实战误区
c#·.net
RReality3 小时前
【Unity Shader URP】平面反射(Planar Reflection)实战教程
ui·平面·unity·游戏引擎·图形渲染·材质