[英]How can I change the lights moving direction from the current light?
The problem is in the LightsEffectCore when I make the array reverse. 当我使阵列反向时,问题出在LightsEffectCore中。 The lights change direction but not from the current light it is on. 灯光会改变方向,但不会改变当前的灯光。 It's jumping to some other light index and change the direction from there. 它正在跳转到其他光线指标,并从那里改变方向。
And I want it to change the direction from the current light index. 我希望它改变当前光指数的方向。 If the light is now on index 5 and I change the direction then move from 5 to 4 and if I changed the direction again move from 4 to 5. 如果光线现在在索引5上并且我改变方向,则从5移至4,如果我改变方向,则再次从4移至5。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsEffect : MonoBehaviour
{
public List<UnityEngine.GameObject> waypoints = new List<UnityEngine.GameObject>();
public int howmanylight = 5;
public Generatenumbers gn;
public bool changeLightsDirection = false;
public float delay = 0.1f;
private List<UnityEngine.GameObject> objects;
private Renderer[] renderers;
private int greenIndex = 0;
private float lastChangeTime;
private void Start()
{
objects = new List<UnityEngine.GameObject>();
if (howmanylight > 0)
{
UnityEngine.GameObject go1 = UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Sphere);
duplicateObject(go1, howmanylight);
LightsEffects();
}
}
private void Update()
{
LightsEffectCore();
}
public void duplicateObject(UnityEngine.GameObject original, int howmany)
{
howmany++;
for (int i = 0; i < waypoints.Count - 1; i++)
{
for (int j = 1; j < howmany; j++)
{
Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
UnityEngine.GameObject go = Instantiate(original, new Vector3(position.x, 0, position.z), Quaternion.identity);
go.transform.localScale = new Vector3(0.3f, 0.1f, 0.3f);
objects.Add(go);
}
}
}
private void LightsEffects()
{
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;
}
private void LightsEffectCore()
{
// 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;
if (changeLightsDirection == true)
{
Array.Reverse(renderers);
changeLightsDirection = false;
}
greenIndex = (greenIndex + 1) % renderers.Length;
renderers[greenIndex].material.color = Color.green;
}
}
}
You have to change your greenIndex as well, when you reversing the array. 反转数组时,还必须更改greenIndex。 If you do not, then the new value at that index is going to be the one after the reverse, and that is why, it looks like it is jumping away. 如果您不这样做,那么该索引处的新值将是相反的值,这就是为什么它看起来像在跳开。 If you want to avoid it jumping about, set the index to greenIndex=renderers.Lenght-greenIndex. 如果要避免它跳来跳去,请将索引设置为greenIndex = renderers.Lenght-greenIndex。
I could not test it out, please correct me if I am wrong. 我无法对其进行测试,如果我错了,请纠正我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.