加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从位置找到GameObject上最近/最近的点?

发布时间:2020-12-16 01:26:22 所属栏目:百科 来源:网络整理
导读:我想在吊臂上找到一个悬挂在起重机上的点,它与起重机的距离最小,起重机臂上有BoxCollider,我正在使用Physics.overlap方法. 如何从源对象中找到GameObject上最近的点? [1]:https://i.stack.imgur.com/ZBRqm.png 解决方法 您可以使用 Collider.ClosestPoint
我想在吊臂上找到一个悬挂在起重机上的点,它与起重机的距离最小,起重机臂上有BoxCollider,我正在使用Physics.overlap方法.

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

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

解决方法

您可以使用 Collider.ClosestPointCollider.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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读