选择移动游戏对象的困难
本文关键字:对象 移动 游戏 选择 | 更新日期: 2023-09-27 18:26:43
我正在开发一款带有触摸控制的手机游戏。我可以很容易地选择一个不移动的游戏对象,它会做出反应,但当它移动时,很难选择它,因为它有点小(例如下落,它都是基于物理的)。有没有办法增加游戏对象的触摸半径,这样可以更容易地按下它,或者有其他解决方案?
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);
}
}
}
请注意,如果你有大量可选择的对象相互对齐,这就不太好了。。。但话说回来,在这种情况下,增加对撞机的尺寸也无济于事。(我想说只是增加对象大小。否则无法提高用户的准确性。或者允许多选,并使用Physics2D.CircleCastAll
)。
希望这能有所帮助!如果你有任何问题,请告诉我。
编辑:为了获得更好的准确性,由于Physics2D.CircleCast
返回的"第一个"结果可以任意选择,因此您可以使用Physics2D.CircleCastAll
获取触摸半径内的所有对象,并且只选择最接近原始触摸点的对象:
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);
}
}
}