c# - 无法解决 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);
}
}
解决方案
推荐阅读
- python - Flask、flask_login、pytest:如何设置flask_login 的current_user?
- r - 加载命名空间中的错误(i,c(lib.loc,.libPaths()),
- javascript - 如何从javascript中的URL中提取最后一个目录和文件名?
- python - pyinstaller 不能在带有烧瓶应用程序的 Linux 上工作
- coq - 如何在 Ltac 中尝试一种策略,但如果失败则继续
- mongodb - MongoDb - 使用哈希映射来减少索引大小?
- html - 如何修复我的 CSS 和 HTML 中的图像网格
- javascript - 在 Javascript 中将数据可视化为颜色条
- sorting - 如何按字母顺序对连接列进行排序
- javascript - 在检查器中调试 r2d3 (R) - 无法在控制台中查看数据?