获取对撞机的最近点

时间:2019-06-15 15:26:39

标签: c# unity3d

我想知道如何获得此功能在Unity 2018.3中尚不可用的相同结果:

https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Collider2D.ClosestPoint.html

1 个答案:

答案 0 :(得分:4)

我今天遇到了这个问题,我也在使用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/