using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
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;
public Vector3 lightsScale;
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++;
howmany = howmany / waypoints.Count;
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 = lightsScale;
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 = renderers.Length - 1 - greenIndex;
}
greenIndex = (greenIndex + 1) % renderers.Length;
renderers[greenIndex].material.color = Color.green;
}
}
public void LightsEffectCore(float delay, ref bool isReversed)
{
// 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 (isReversed)
{
renderers.Last().material.color = Color.green;
greenIndex = renderers.Length - 1;
}
else
{
renderers.First().material.color = Color.green;
greenIndex = 0;
}
greenIndex = (greenIndex + 1) % renderers.Length;
renderers[greenIndex].material.color = Color.green;
}
}
}
This way the script is now it’s creating two spheres between 3 waypoints.
From waypoints index 0 to waypoints index 1 between this two it’s creating two spheres.
From waypoints index 1 to waypoints index 2 also two spheres.
In the original howmany value is 11 and after the divide howmany value is 3.
So it should create 3 spheres between each two waypoints.
And if there are 3 waypoints or more I also want it to create spheres between the last waypoint and the first one for example between index 3 and index 0.
In the screenshot the left the red circle sphere is index 0 on the right the black is index 1 and blue is index 2.
There are 2 red spheres between index 0 and 1 and two red spheres between index 1 and 2.
But I want also more 2 red spheres between the blue and red (index 2 and index 0).
And then if I change howmany for example to 30 or to 23 or to 76 same idea to create equal numbers of spheres between each two waypoints. If there are 3 waypoints or 30 waypoints.
Now I did a test changed howmany to 21. So in my logic it should create between each two waypoints 7 new spheres. From index 0 to 1 from 1 to 2 and from 2 to 0. But instead it’s creating 6 new spheres between 0 and 1 and 1 and 2 overall 12 spheres.