首页 > 解决方案 > 无法解决 Unity 制作的手机游戏中的 hitbox 问题

问题描述

我正在开发一个游戏,其中物体从屏幕顶部掉落,您尝试在它们到达屏幕底部之前点击并点击它们。当我在编辑器中玩游戏时,代码运行良好(触摸控制切换到鼠标控制),除了当我在手机上运行游戏时,如果你在物体在其行进的方向上,并且如果您向物体的后端或中心轻敲,则不会记录到命中。我现在已经构建并运行了 10 多次游戏,每次都试图解决这个问题,但似乎没有任何帮助。我目前的理论是,我的触摸控件代码有太多的事情发生和/或有冗余,当它检查对象是否在触摸位置时,对象已移动到其他位置。关于为什么点击框关闭的任何想法,是否有更好的方法来使用触摸屏进行点击检测?

   void FixedUpdate()
     {
         if (IsTouch())
         {
             CheckTouch(GetTouchPosition());
         }
     }


    // Returns true if the screen is touched
         public static bool IsTouch()
         {
             if (Input.touchCount > 0)
             {
                 if (Input.GetTouch(0).phase == TouchPhase.Began)
                 {
                     return true;
                 }
             }
             return false;
         }


     // Gets the position the touch
         private Vector2 GetTouchPosition()
         {
             Vector2 touchPos = new Vector2(0f, 0f);
             Touch touch = Input.GetTouch(0);

             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 touchPos = touch.position;
             }

             return touchPos;
         }

     // Checks the position of the touch and destroys the ball if the 
        ball is touched
         private void CheckTouch(Vector2 touchPos)
         {
             RaycastHit2D hit = 
             Physics2D.Raycast(Camera.main.ScreenToWorldPoint(
             (Input.GetTouch(0).position)), Vector2.zero);

             if (hit.collider != null)
              {
                    destroy(hit.collider.gameObject);
              }
     }

标签: c#unity3dmobile

解决方案


推荐阅读