Unity 获取离某个物体最近

public Transform pos;
public Transform pos1;
public float value = 0;
public float value1 = 0;

//坐标和值
// Update is called once per frame
void Update()
{

//判断离本物体最小的值  就是最近
if (value<value1)
{
print(pos.name);
}
else
{
print(pos1.name);
}
value = (transform.position - pos.position).sqrMagnitude;
value1 = (transform.position - pos1.position).sqrMagnitude;

获取所以物体的坐标离本物体的距离
}

 

 通过获取所以物体离本物体的距离判断最小的距离值

原文地址:https://www.cnblogs.com/suiyi78/p/12953807.html