简体   繁体   中英

How can I change the lights moving direction from the current light?

The problem is in the LightsEffectCore when I make the array reverse. 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.

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. 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.

I could not test it out, please correct me if I am wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM