我想知道如何在Unity 2018.3中获得此函数提供的相同结果,但该函数尚不可用:
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Collider2D.ClosestPoint.html
最佳答案
我今天遇到了这个问题,我也在使用2018.3。这是一个代替collider2D.ClosestPoint的帮助函数。根据需要编辑对撞机。
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;
}
}
从此线程获取它:https://forum.unity.com/threads/get-closest-point-of-collider2d.225120/