选择移动游戏对象的困难

Difficulties selecting a moving gameobject

我正在开发一款带有触摸控制的手机游戏。我可以非常轻松地 select 一个不移动的游戏对象并且它会响应,但是当它移动时很难 select 它,因为它有点小(例如掉落,它都是基于物理的)。有没有办法增加游戏对象的触摸半径,以便更容易按下,或者有其他解决方案?

 private void Update() {
    //User input (touches) will select cubes
    if(Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);
    }
    Touch[] touches = Input.touches;

    foreach(var touchInput in touches) {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);

        if (hit.collider != null) {
            selectedCube = hit.collider.gameObject;
            selectedCube.GetComponent<SpriteRenderer>().color = Color32.Lerp(defaultColor, darkerColor, 1);
        }

    }
}

与其增加对象的碰撞器大小(您在评论中讨论过),不如以相反的方式处理这个问题如何?使用 Physics2D.CircleCast:

检查触摸周围的区域是否发生碰撞,而不仅仅是单个点
private void Update() {
    //User input (touches) will select cubes
    if(Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);
    }
    Touch[] touches = Input.touches;

    foreach(var touchInput in touches) {
        float radius = 1.0f; // Change as needed based on testing
        RaycastHit2D hit = Physics2D.CircleCast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), radius, Vector2.zero);

        if (hit.collider != null) {
            selectedCube = hit.collider.gameObject;
            selectedCube.GetComponent<SpriteRenderer>().color = Color32.Lerp(defaultColor, darkerColor, 1);
        }

    }
}

请注意,如果您有大量 select 可用对象相互齐平,这将不会很好...但是话又说回来,在这种情况下增加对撞机的大小也无济于事。 (我想说只是增加对象大小。没有办法以其他方式提高用户准确性。或者允许 multi-select,并使用 Physics2D.CircleCastAll)。

希望对您有所帮助!如果您有任何问题,请告诉我。

编辑: 为了获得更好的准确性,由于 Physics2D.CircleCast 返回的 "first" 结果可能是任意 selected,您可以改用Physics2D.CircleCastAll获取触摸半径内的所有对象,只select最接近原始触摸点的对象:

private void Update() {
    //User input (touches) will select cubes
    if(Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);
    }
    Touch[] touches = Input.touches;

    foreach(var touchInput in touches) {
        float radius = 1.0f; // Change as needed based on testing
        Vector2 worldTouchPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
        RaycastHit2D[] allHits = Physics2D.CircleCastAll(worldTouchPoint, radius, Vector2.zero);

        // Find closest collider that was hit
        float closestDist = Mathf.Infinity;
        GameObject closestObject = null;
        foreach (RaycastHit2D hit in allHits){

            // Record the object if it's the first one we check,
            // or is closer to the touch point than the previous
            if (closestObject == null ||
                Vector2.Distance(closestObject.transform.position, worldTouchPoint) < closestDist){
                closestObject = hit.collider.gameObject;
                closestDist = Vector2.Distance(closestObject.transform.position, worldTouchPoint);
            }
        }

        // Finally, select the object we chose based on the criteria
        if (closestObject != null) {
            selectedCube = closestObject;
            selectedCube.GetComponent<SpriteRenderer>().color = Color32.Lerp(defaultColor, darkerColor, 1);
        }
    }
}