我怎样才能在每个方向改变方向? [英] How can i change direction each time one direction ?

查看:78
本文介绍了我怎样才能在每个方向改变方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System; 
使用System.Collections;
使用System.Collections.Generic;使用UnityEngine
;

公共类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;
}

//将绿色设置为第一个
greenIndex = 0;
renderers [greenIndex] .material.color = Color.green;
}

///< summary>
///运行效果
///< / summary>
///< param name =" changeDirection">更改灯光移动方向 - false = forward,true = backward。< / param>
public void LightsEffectCore(float delay,bool changeDirection)
{
//每次更改颜色`delay`秒
if(Time.time> lastChangeTime + delay)
{
lastChangeTime = Time.time;

//将最后一个渲染器的颜色设置为红色
//,将当前渲染器的颜色设置为绿色
渲染器[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我需要再次反转数组但是因为它是
false它将一直反转数组:


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





但现在它将变为false或者游戏开始时为false它会立即改回true并反转数组等等。



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


也许我需要另一面旗帜?

解决方案

changeDirection应该是一个事件,而不是一个变量,当某些事情发生时表明方向需要改变。如果你不能作为一个事件执行某事,那么是的,你需要两个标志;一个表示当前方向,另一个表示方向需要更改


注意以下是一个有用的技巧;您可以使用以下方式切换标志的值:

 flag =!flag; 


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;
        }
    }
}

The part of the direction change:

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

The problem is that now it will be true all the time and next time it will be changed to false i will need to reverse the array again but then since it's false it will reverse the array all the time:

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


But now when it will be change back to false or if the game start as false it will change right back to true and will reverse the array and so on.

I want that when it's true keep it true and once changing it to false keep it false until changing again. I got messed the flag states and the reversing.

Maybe i need another flag ?

解决方案

changeDirection should be an event, not a variable, that is executed when something happens that indicates the direction needs to change. If you can't execute something as an event then yes you need two flags; one to indicate the current direction and another that indicates that the direction needs to change.

Note that the following is a useful trick; you can toggle the value of a flag using:

flag = !flag;


这篇关于我怎样才能在每个方向改变方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆