简介:
1,通过ClosestPoint API获得物体A碰撞体 到物体B最短的那个点。即物体A表面到物体B最短距离的点。
2,同理获得物体B表面到物体A最短距离的点。
3,两个点之间的距离也就是物体表面最近的两个点。
用途:实现塞尔大:王国之泪中的粘接功能可能需要。
DrawClosestPoints .cs
using UnityEngine;
public class DrawClosestPoints : MonoBehaviour
{
public GameObject object1;
public GameObject object2;
void Update()
{
// 获取物体表面上最近的两个点
Vector3 closestPointOnObject1 = object1.GetComponent<Collider>().ClosestPoint(object2.transform.position);
Vector3 closestPointOnObject2 = object2.GetComponent<Collider>().ClosestPoint(object1.transform.position);
// 画一条线连接这两个点
Debug.DrawLine(closestPointOnObject1, closestPointOnObject2, Color.red);
}
}