大家好。有一个代码可以找到物体之间的最小距离并选择该物体以便在游戏中进行进一步的交互。
我看到并知道代码看起来很糟糕,但我不知道如何正确缩短它(例如,使用 foreach)。
请帮助我,但不要冒犯我)
代码:
void Distanse()
{
if (bots[0] != null)
{
if (players[0] != null)
{
float tmp1 = Vector3.Distance(players[0].transform.position, bots[0].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[0];
}
}
if (players[1] != null)
{
float tmp1 = Vector3.Distance(players[1].transform.position, bots[0].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[0];
}
}
if (players[2] != null)
{
float tmp1 = Vector3.Distance(players[2].transform.position, bots[0].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[0];
}
}
}
if (bots[1] != null)
{
if (players[0] != null)
{
float tmp1 = Vector3.Distance(players[0].transform.position, bots[1].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[1];
}
}
if (players[1] != null)
{
float tmp1 = Vector3.Distance(players[1].transform.position, bots[1].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[1];
}
}
if (players[2] != null)
{
float tmp1 = Vector3.Distance(players[2].transform.position, bots[1].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[1];
}
}
}
if (bots[2] != null)
{
if (players[0] != null)
{
float tmp1 = Vector3.Distance(players[0].transform.position, bots[2].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[2];
}
}
if (players[1] != null)
{
float tmp1 = Vector3.Distance(players[1].transform.position, bots[2].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[2];
}
}
if (players[2] != null)
{
float tmp1 = Vector3.Distance(players[2].transform.position, bots[2].transform.position);
if (tmp1 < distance)
{
distance = tmp1;
botNearest = bots[2];
}
}
}