0

我统一制作了一个 2d 游戏,我需要你的帮助。

例子

游戏是如图所示随机创建9个彩色点。玩家必须将点与相应的颜色连接起来。这都是我做的。

问题: 当我单击一个点并立即移动到另一个进行连接时,它不会立即连接,而是在大约 1 秒后连接。如果我单击一个点并按住大约 1 秒钟,然后快速移动到下一个点,它就会连接。

也就是说,我需要等待第一点,或者等待第二点。

事实证明,这一切都归功于对撞机和 Raycast。他没有时间检查。

我的代码示例(非常简短)

Vector3 startPos;
Vector3 endPos;

Vector3 mousePos;
Vector2 mousePos2D;
RaycastHit2D hit;

void Update() {

mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePos2D = new Vector2 (mousePos.x, mousePos.y);
hit = Physics2D.Raycast (mousePos2D, Vector2.zero);

// Here comes the creation of the first point and the connection 
if (Input.GetMouseDown(0)) {
if (hit.collider != null) {
color = hit.collider.tag;
startPos = hit.collider.transform.position;
endPos = mousePos2D;
}
}

// Here comes the creation line between dots 
if (Input.GetMouse(0)) {
if (hit.collider != null) {
if (hit.collider.tag == color) {
endPos = hit.collider.transform.position;
// Here is instantiated new line and with startPos and endPos and I set
startPos = endPos;
endPos = mousePos2D;
}
}
}

// Here points are deleted if connected 
if (Input.GetMouseUp(0)) {
    // Nothing important
}
}

问题是:在“Input.GetMouseDown(0)”中,即使鼠标经过对象“hit.collider is equal null”,大约1秒后,hit.collider不为null并给出颜色标记。

视频示例: https ://www.youtube.com/watch?v=8x6TSBfBIzc

所有代码脚本: https ://drive.google.com/open?id=1QKRInr26acu-E4zXPUf-DbW0h9zbsnUr

如何用期望解决这个问题?为什么不能立即连接?

4

0 回答 0