2016-01-15 96 views
2

我正在开发一个应用程序,它有许多可以浮动但不会相互碰撞的球。这意味着它们重叠很多。我已经得到了它,所以如果你点击/触摸球,它们会被销毁。但是,如果一个球在另一个球之后,那么这个球不会被销毁,直到前面的球被清除。在Unity中检测重叠2D对象上的输入碰撞

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); 
        } 

       } 
      } 

     } 

    } 
} 

而且在球上,我只是有:

由@Savlon提供
void OnTouchDown() { 

    KillBall(); 
} 

void OnTouchStay() { 

    KillBall(); 

} 
+0

所有球都在同一个z位置? – Everts

+3

如果您想要删除手指/鼠标下方的所有球,请使用RaycastAll而不是Raycast - http://docs.unity3d.com/ScriptReference/Physics2D.RaycastAll.html您应该能够遍历所有返回的RaycastHit2D并摧毁他们的游戏对象。 – Savlon

+0

是的,所有的球都在同一个z,我会检查出RaycastAll .. – dolyth

回答

1

答案,所以感谢您的:)

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); 

      } 


     }