- 
      
 - 
        
Save gabrieldevsouza/5c4d43b79127a209d33fffee02069e5d to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | using UnityEngine; | |
| public static class Collider2DExtension | |
| { | |
| /// <summary> | |
| /// Return the closest point on a Collider2D relative to point | |
| /// </summary> | |
| public static Vector2 ClosestPoint(this Collider2D col, Vector2 point) | |
| { | |
| GameObject go = new GameObject("tempCollider"); | |
| go.transform.position = point; | |
| CircleCollider2D c = go.AddComponent<CircleCollider2D>(); | |
| c.radius = 0.1f; | |
| ColliderDistance2D dist = col.Distance(c); | |
| Object.Destroy(go); | |
| return dist.pointA; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment