我想在吊臂上找到一个悬挂在起重机上的点,它与起重机的距离最小,起重机臂上有BoxCollider,我正在使用Physics.overlap方法. 如何从源对象中找到GameObject上最近的点? [1]:https://i.stack.imgur
如何从源对象中找到GameObject上最近的点?
[1]:https://i.stack.imgur.com/ZBRqm.png
您可以使用Collider.ClosestPoint和
Collider.ClosestPointOnBounds执行此操作.如果您还想检查自定义位置和旋转而不是使用对撞机的位置和旋转,请使用
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);
}
