【问题标题】:Raycast Not Working on Phone but working on Unity EditorRaycast 不在电话上工作,但在 Unity 编辑器上工作
【发布时间】:2021-12-06 12:40:48
【问题描述】:

当我在统一编辑器上运行时,光线投射完美运行。但是当我在“构建并运行”期间在移动设备上运行时,光线投射不会击中任何东西。我知道它什么都没有,因为我放置了一个 3dtext 来表示是否有热。

我使用 3d 文本来比较 Unity 编辑器和手机中产生的光线。它们产生相同的光线。

我以前使用过这段代码,它运行良好。它适用于统一编辑器,但不适用于手机。我错过了什么吗?

我正在使用三星 S10 进行测试。

            //Get input
            if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
            {

                if (!IsCurrentlyInTouch)
                {

                    if (Input.touchCount > 0)
                    {
                        Touch touch = Input.GetTouch(0);
                        ray = Camera.main.ScreenPointToRay(touch.position);
                    }
                    else
                    {//keyboard
                        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    }
                    
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit,Mathf.Infinity))
                    {
                        selectedObject = hit.transform;
                        //Do things
                    }



                    if (hit.collider != null)
                    {
                        DebugTxt.text = hit.collider.name;
                    }
                    else
                    {
                        DebugTxt.text = "no hit " + ray.GetPoint(10) ;
                    }

                }
                IsCurrentlyInTouch = true;
            }
            else
            {
                IsCurrentlyInTouch = false;
            }

【问题讨论】:

    标签: c# android unity3d mobile


    【解决方案1】:

    从这个 youtube 视频中找到了答案。 https://www.youtube.com/watch?v=DFbrSa9qjbk&list=LL&index=1&ab_channel=unityfy

    问题不在代码中。那是我切换到 IL2CPP 的时候。由于某种原因,当我切换到 IL2CPP 时,光线投射会受到影响。

    要解决,取消勾选播放器设置-->其他设置-->优化下的“Strip Engine Code”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多