Skip to content

Instantly share code, notes, and snippets.

@gabrieldevsouza
Forked from Wolfos/ClosestPoint.cs
Created February 7, 2019 03:55
Show Gist options
  • Save gabrieldevsouza/5c4d43b79127a209d33fffee02069e5d to your computer and use it in GitHub Desktop.
Save gabrieldevsouza/5c4d43b79127a209d33fffee02069e5d to your computer and use it in GitHub Desktop.
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