在 Unity 中检测重叠二维对象的输入碰撞
Detecting input collision on overlapping 2D objects in Unity
我正在开发一个应用程序,其中有许多球漂浮在周围但不会相互碰撞。这意味着它们有很多重叠。我明白了,如果你 click/touch 球,它们就会被摧毁。但是,如果一个球在另一个球的后面,则在清除前面的球之前,这个球不会被破坏。
上图显示了我正在寻找的内容,如果用户 clicks/touches 在位置 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);
}
}
我正在开发一个应用程序,其中有许多球漂浮在周围但不会相互碰撞。这意味着它们有很多重叠。我明白了,如果你 click/touch 球,它们就会被摧毁。但是,如果一个球在另一个球的后面,则在清除前面的球之前,这个球不会被破坏。
上图显示了我正在寻找的内容,如果用户 clicks/touches 在位置 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);
}
}