简体   繁体   English

如何从当前光源改变光源的移动方向?

[英]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.

相关问题 如何使二维对象不断朝一个方向移动,但在给定水平输入时改变方向但不改变速度? - How can I make 2d object constantly moving in one direction, but when given horizontal input change direction but not change speed? 交通灯模拟:改变交通灯的颜色 - Traffic lights simulation: change the color of the traffic light 发生碰撞时如何阻止播放器向某个方向移动 - How can i stop my player from moving in a direction when a collision is made 如何更改图像填充量方向? - How can I change image fillamount direction? 如何更改Word中文本的方向? - How can I change direction of the text in Word? 如何使用标志简化脚本? 以及如何使淡入/淡出从当前点继续到中断方向? - How can I simplify the script using the flags? and how can I make the fade in/out continue from the current point to the interrupted direction? 来自光源RayTracing的光的方向 - Direction Of Light From LightSource RayTracing 如何更改抽绳中文本的流动方向? - How can I change the direction text flows in a drawstring? 如何在 wpf helix 工具包中更改默认相机方向 - how can i change default camera direction in wpf helix toolkit 如何使物体像愤怒的小鸟2上的小鸟一样面对运动的方向? - How can I make an object to face the direction it's moving like the birds at angry birds 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM