【发布时间】:2018-05-25 20:03:48
【问题描述】:
我有这段代码可以计算出我的白球和绿球之间的最短距离。
当我的绿色球使用它时,它们会追逐白色并将它们变成绿色。
有没有办法与我的绿球相反,让我的白球跑掉?绿球代码如下:
private Vector3 FindClosestHuman()
{
GameObject[] Humans = GameObject.FindGameObjectsWithTag("Human");
Transform bestTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = this.transform.position;
foreach (GameObject human in Humans)
{
Vector3 directionToTarget = human.transform.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dSqrToTarget < closestDistanceSqr)
{
closestDistanceSqr = dSqrToTarget;
bestTarget = human.transform;
}
}
return bestTarget.position;
}
void Update ()
{
infectionAgent.SetDestination(FindClosestHuman());
}
我尝试在 Update 方法中使用 transform.translate 和 Vector3,但我似乎无法让它工作......
【问题讨论】:
-
如果你这样做并且你不能做相反的事情意味着这段代码中有很多复制和粘贴:D
-
@IgnacioSolerGarcia 你是对的哈哈。问题是我现在需要做的似乎不适合该代码,我不知道 SetDestination 的反面是什么。
-
你能说得更具体点吗?你想让球从最近的球员身上跑掉还是别的什么?