我怎样才能在每个方向改变方向?

时间:2017-11-21 18:35:01

标签: c# unity3d unity5

剧本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LightsEffects : MonoBehaviour
{
    private Renderer[] renderers;
    private float lastChangeTime;
    private int greenIndex = 0;

    public void LightsEffect(List<GameObject> objects, Color instantiateColor, Color colorEffect)
    {
        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;
    }

    /// <summary>
    /// Running the effect
    /// </summary>
    /// <param name="changeDirection">Changing the lights movement directions - false = forward, true = backward.</param>
    public void LightsEffectCore(float delay, bool changeDirection)
    {
        // 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 (changeDirection == true)
            {
                Array.Reverse(renderers);
                changeDirection = false;
            }
            greenIndex = (greenIndex + 1) % renderers.Length;
            renderers[greenIndex].material.color = Color.green;
        }
    }
}

方向改变的部分:

if (changeDirection == true)
                {
                    Array.Reverse(renderers);
                    changeDirection = false;
                }

问题是现在它一直都是真的,下次它会被改为false我需要再次反转数组但是因为它是假的它会一直反转数组:

if (changeDirection == true)
            {
                Array.Reverse(renderers);
                changeDirection = false;
            }
            else
            {
                Array.Reverse(renderers);
                changeDirection = true;
            }

但是现在当它改变为假或者如果游戏开始为假时,它将改回到true并将反转数组等等。

我希望当它真实时保持真实并且一旦将其改为假就保持错误直到再次改变。 我弄乱了旗帜状态和逆转。

0 个答案:

没有答案