【问题标题】:Find a nearest/closest point on a GameObject from location?从位置找到游戏对象上最近/最近的点?
【发布时间】:2018-04-02 14:58:11
【问题描述】:

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

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

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

【问题讨论】:

  • 你有没有尝试过?可以拍张照片吗?
  • 可能是图片可以帮助理解我的问题。

标签: c# unity3d unity-webgl


【解决方案1】:

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

【讨论】:

  • closestPointCollider 返回起重机上方的吊臂部分之外的一个点。
猜你喜欢
  • 2019-04-02
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-13
  • 2020-11-16
  • 2021-11-27
相关资源
最近更新 更多