public static bool IsUnderPoint(this GameObject gameObject, Vector2 point) { BoxCollider2D boxCollider2D = gameObject.GetComponent <BoxCollider2D> (); if (boxCollider2D != null) { Rect r = RectExtensions.FromCollider(boxCollider2D); return(r.Contains(point)); } CircleCollider2D circleCollider2D = gameObject.GetComponent <CircleCollider2D> (); if (circleCollider2D != null) { Vector2 dist = ((Vector2)circleCollider2D.transform.position + circleCollider2D.offset) - point; return(dist.magnitude < circleCollider2D.radius); } Debug.LogWarning("IsUnderPoint : No collider on object " + gameObject.name); return(false); }
public static GameObject GetChildUnderPoint(this GameObject gameObject, Vector2 point) { int count = gameObject.transform.childCount; for (int i = 0; i < count; i++) { GameObject child = gameObject.transform.GetChild(i).gameObject; BoxCollider2D boxCollider2D = child.GetComponent <BoxCollider2D> (); if (boxCollider2D != null) { Rect r = RectExtensions.FromCollider(boxCollider2D); // Debug.Log ( "Rect : " + r.ToString() + " - Point : " + point.ToString() ); if (r.Contains(point)) { return(child); } } } return(null); }