// This converts a BoxCollider2D to a rectangle using the object's actual position public static Rect BoxToRect( this GameObject self ) { return self.BoxToRectAtPosition( self.transform.position ); }
// This returns the first dictionary object you would be colliding with at a specific position. It's helpful in // character controllers. public static GameObject whichDictionaryObjectCollidesAtPosition( this GameObject self, Dictionary<GameObject, Rect> colliderDictionary, Vector3 position ) { GameObject collisionObject = null; Dictionary<GameObject, Rect> colliderDictionaryCopy = new Dictionary<GameObject, Rect>(colliderDictionary); Rect testCollider = self.BoxToRectAtPosition( position ); foreach (var pair in colliderDictionaryCopy) { if ( self != pair.Key ) { // Check to see if these are colliding, and store the first object involved in a collision for return. if (Intersecting( testCollider, pair.Value )) { if ( collisionObject == null ) collisionObject = pair.Key; // not all collision checks need announcements to the objects that a collision took place. // sometimes we just need to know if a collision occurred. if ( collisionAnnouncements ) AnnounceCollision( self, pair.Key ); } } // turn this back off so that the next collision check doesn't send announcements, unless it intends to. collisionAnnouncements = false; } return collisionObject; }