从位置找到 GameObject 上的 nearest/closest 点?

Find a nearest/closest point on a GameObject from location?

我想从起重机上悬挂的负载找出吊臂部分上的一个点,该点与负载的距离最小,起重机吊臂有 BoxCollider 命中,我正在使用 Physics.overlap 方法。

如何从源对象找到 GameObject 上的最近点?

[1]: https://i.stack.imgur.com/ZBRqm.png

你可以用 Collider.ClosestPoint and Collider.ClosestPointOnBounds. If you also want to check for custom position and rotation instead of using the collider's postion and roation then use Physics.ClosestPoint 来做到这一点。

其中 3 个函数的用法示例:

public Vector3 sourceObject;
public Collider targetCollider;

// Update is called once per frame
void Update()
{
    //Method 1
    Vector3 closestPoint = targetCollider.ClosestPoint(sourceObject);

    //Method 2
    Vector3 closestPointInBounds = targetCollider.ClosestPointOnBounds(sourceObject);

    //Method 3
    Vector3 pos = targetCollider.transform.position;
    Quaternion rot = targetCollider.transform.rotation;
    Vector3 closestPointCollider = Physics.ClosestPoint(sourceObject, targetCollider, pos, rot);
}