none
How can i change direction each time one direction ? RRS feed

  • Question

  • 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 ?

    Tuesday, November 21, 2017 10:21 PM

Answers

  • Hello,

     Since you are using object types bool, you can simply XOR and that should solve.

     changeDirection |= true;

    If you had object types like Int, you could do

    changeDirection = 1 - changeDirection;

      

     Hope this helps :)


    • Edited by User3DX Wednesday, November 22, 2017 11:02 AM logic corrected
    • Proposed as answer by Fei HuModerator Sunday, November 26, 2017 11:22 AM
    • Marked as answer by Rosemerysalvedora Saturday, July 4, 2020 9:08 PM
    Tuesday, November 21, 2017 10:39 PM

All replies

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



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, November 21, 2017 10:35 PM
  • Hello,

     Since you are using object types bool, you can simply XOR and that should solve.

     changeDirection |= true;

    If you had object types like Int, you could do

    changeDirection = 1 - changeDirection;

      

     Hope this helps :)


    • Edited by User3DX Wednesday, November 22, 2017 11:02 AM logic corrected
    • Proposed as answer by Fei HuModerator Sunday, November 26, 2017 11:22 AM
    • Marked as answer by Rosemerysalvedora Saturday, July 4, 2020 9:08 PM
    Tuesday, November 21, 2017 10:39 PM