为什么里面有两个循环的 ienumerator 只工作一次?
Why the ienumerator with the two loops inside is working only once?
开始:
StartCoroutine(moveStuff());
然后:
IEnumerator moveStuff()
{
for (int i = 0; i < allLines.Count; i++)
{
while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
{
counter++;
endPos = allLines[i].GetComponent<EndHolder>().EndVector;
Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);
instancesToMove[i].transform.position =
Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
//move towards destination
yield return null;
}
}
}
在使用断点的第一次迭代中,我可以看到来自 allLines 和 instancestoMove 的第一个对象正在移动。但是然后移动下一个对象,它只是显示列表中的所有其余对象,就像它们已经被移动一样。
列表中的其余对象已经出现在它们的末尾位置。
他们没有像第一个循环那样移动。
allLines 和 instancesToMove 都是 List 类型。
也许您应该在 while 语句之前将计数器重置为零。
否则 counter / 25f * speed 会无限增长,并且在后续行中越来越高。
开始:
StartCoroutine(moveStuff());
然后:
IEnumerator moveStuff()
{
for (int i = 0; i < allLines.Count; i++)
{
while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
{
counter++;
endPos = allLines[i].GetComponent<EndHolder>().EndVector;
Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);
instancesToMove[i].transform.position =
Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
//move towards destination
yield return null;
}
}
}
在使用断点的第一次迭代中,我可以看到来自 allLines 和 instancestoMove 的第一个对象正在移动。但是然后移动下一个对象,它只是显示列表中的所有其余对象,就像它们已经被移动一样。
列表中的其余对象已经出现在它们的末尾位置。 他们没有像第一个循环那样移动。
allLines 和 instancesToMove 都是 List 类型。
也许您应该在 while 语句之前将计数器重置为零。 否则 counter / 25f * speed 会无限增长,并且在后续行中越来越高。