我试图运行一个脚本,它会在点a和b之间的接触点上产生“斜坡”。该代码会收到一个列表,列出坡道的元素应该在哪里,然后立即将它们放置在屏幕上。协程只发射一次
但是,协程只运行一次,我不明白为什么。任何人都可以给我一些建议吗?
非常感谢你提前
public IEnumerator CreateRamp(List<Vector3> lP, float angle)
{
int i = 1;
while (i <= lP.Count)
{
Debug.Log("Iteration " + i + " of " + lP.Count + " position is " + lP[i]);
GameObject item = Instantiate(Resources.Load("floor")) as GameObject;
item.transform.position = current_Position;
item.transform.eulerAngles = new Vector3(0, 0, UnityEngine.Random.Range(0f, 360f));
item.GetComponent<Ramp>().strtPos = item.transform.position;
item.GetComponent<Ramp>().strtRot = item.transform.eulerAngles;
item.GetComponent<Ramp>().strtScale = new Vector3(0.4f, 0.4f, 1);
item.GetComponent<Ramp>().tgtRot = new Vector3(0, 0, angle);
item.GetComponent<Ramp>().tgtPos = lP[i-1];
i += 1;
yield return new WaitForSeconds(0.2f);
}
}
那么,lP.Count的返回是什么? –
与你的问题没有关系,但你应该在每个循环调用'.GetComponent()'并将结果保存在局部变量中,然后让5个调用使用该变量。 –
您是否使用StartCoroutine运行此方法?如果您将其作为常规方法调用:CreateRamp(...),它将只执行一次。 –