gpt4 book ai didi

c# - 在 Unity 中检测重叠二维对象的输入碰撞

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:18 24 4
gpt4 key购买 nike

我正在开发一个应用程序,其中有许多球漂浮在周围但不会相互碰撞。这意味着它们有很多重叠。我明白了,如果你点击/触摸球,它们就会被摧毁。但是,如果一个球在另一个球的后面,则在前面的球被清除之前,这个球不会被摧毁。

enter image description here

上图显示了我正在寻找的内容,如果用户在位置 x 处单击/触摸,则销毁所有对象,而不仅仅是最前面的对象。非常感谢任何帮助。

这是我的输入脚本:

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TouchInput : MonoBehaviour {

public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject> ();
private GameObject[] touchesOld;
private RaycastHit2D hit;

void Update () {


#if UNITY_EDITOR

if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {

touchesOld = new GameObject[touchList.Count];
touchList.CopyTo (touchesOld);
touchList.Clear ();


hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, touchInputMask);
//Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (hit) {

GameObject recipient = hit.transform.gameObject;
touchList.Add (recipient);

if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage ("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage ("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (Input.GetMouseButton(0)) {
recipient.SendMessage ("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);

}

}

foreach (GameObject g in touchesOld) {
if (!touchList.Contains (g)) {
if(g!=null) {
g.SendMessage ("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}

}
}

}

#endif

if (Input.touchCount > 0) {

touchesOld = new GameObject[touchList.Count];
touchList.CopyTo (touchesOld);
touchList.Clear ();

foreach (Touch touch in Input.touches) {

hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, touchInputMask);

if (hit) {

GameObject recipient = hit.transform.gameObject;
touchList.Add (recipient);

if (touch.phase == TouchPhase.Began) {
recipient.SendMessage ("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (touch.phase == TouchPhase.Ended) {
recipient.SendMessage ("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (touch.phase == TouchPhase.Stationary) {
recipient.SendMessage ("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (touch.phase == TouchPhase.Canceled) {
recipient.SendMessage ("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);

}

}

}

foreach (GameObject g in touchesOld) {
if (!touchList.Contains (g)) {
if (g != null) {
g.SendMessage ("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}

}
}

}

}
}

在球上我只是有一个:

    void OnTouchDown() {

KillBall ();
}

void OnTouchStay() {

KillBall ();

}

最佳答案

@Savlon 提供的答案,非常感谢:)

private RaycastHit2D[] hits;

...

hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, touchInputMask);

foreach(RaycastHit2D hit in hits) {

GameObject recipient = hit.transform.gameObject;
touchList.Add (recipient);

if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage ("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage ("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);

}
if (Input.GetMouseButton(0)) {
recipient.SendMessage ("OnTouch",hit.point,SendMessageOptions.DontRequireReceiver);

}


}

关于c# - 在 Unity 中检测重叠二维对象的输入碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34805581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com