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

放肆的年华 提交于 2019-12-01 05:44:47

问题


I want to find out a point on boom section from load hanging on crane which have minimum distance from load, crane-Boom having BoxCollider on hit and I am using Physics.overlap method.

How do you find a closest point on a GameObject from source object?

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


回答1:


You can do that with 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.

Example usage for 3 of these functions:

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);
}


来源:https://stackoverflow.com/questions/49613601/find-a-nearest-closest-point-on-a-gameobject-from-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!