添加一个新的bool变量,该变量将用作向前和向后两个方向之间的标记。
在剧本的顶部:
public float delay = 0.1f;
private Renderer[] renderers;
private float lastChangeTime;
private int greenIndex = 0;
private List<GameObject> objects = new List<GameObject>();
在开始
void Start()
{
LightsEffect();
}
更新内部
void Update()
{
// Change color each `delay` seconds
if (Time.time > lastChangeTime + delay)
{
lastChangeTime = Time.time;
// Set color of the last renderer to red
// and the color of the current one to green
renderers[greenIndex].material.color = Color.red;
greenIndex = (greenIndex + 1) % renderers.Length;
renderers[greenIndex].material.color = Color.green;
}
}
和LightsEffect方法
private void LightsEffect()
{
renderers = new Renderer[objects.Count];
for (int i = 0; i < renderers.Length; i++)
{
renderers[i] = objects[i].GetComponent<Renderer>();
renderers[i].material.color = Color.red;
}
// Set green color to the first one
greenIndex = 0;
renderers[greenIndex].material.color = Color.green;
}
我想要做的是使用一个标志来改变光线的移动方向。