- Home /
 
               Question by 
               Bogdan003 · Apr 07, 2020 at 02:34 PM · 
                colorlightcolor change  
              
 
              Changing light color back and forth
I tried making a script that changes the directional light color when i swipe down and changing it back when i swipe up but after changing it back it starts changing back and forth constantly.
Here is the script
     Color color0 ;
     Color color1 ;
     public float duration;
     Light lt;
     bool swipepossible = false;
     private bool swipingdown = false;
     private bool swipingup = false;
 
 
     // Update is called once per frame
     void Update()
     {   
 
         lt = GetComponent<Light>();
         if (lt.color == new Color(255f / 255f, 244f / 255f, 214f / 255f) || lt.color == new Color(45f / 255f, 67f / 255f, 95f / 255f)) swipepossible = true;
         if (SwipeInput.swipedDown && swipepossible)
         {
             swipingdown = true;
         }
         if (SwipeInput.swipedUp && swipepossible)
         {
             swipingup = true;
         }
         if ( swipingdown == true )
         {  
             Change();
         }
         if (swipingup == true)
         {
             ChangeBack();
         }
     }
     private void Change()
     {
         lt = GetComponent<Light>();
         color0 = lt.color;
         color1 = new Color(45f / 255f, 67f / 255f, 95f / 255f);
         if (lt.color != color1)
         {
             float t = Mathf.PingPong(Time.time, duration) / duration;
             lt.color = Color.Lerp(color0, color1, t);
         }
     }
     private void ChangeBack()
     {
         lt = GetComponent<Light>();
         color0 = lt.color;
         color1 = new Color(255f / 255f, 244f / 255f, 214f / 255f);
         if (lt.color != color1)
         {
             float t = Mathf.PingPong(Time.time, duration) / duration;
             lt.color = Color.Lerp(color0, color1, t);
         }
     }
 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Hellium · Apr 07, 2020 at 02:58 PM
 public Gradient Gradient ; // Provide the colors in the inspector
 public float Duration;
 private Light light;
 private float value;
 private float targetValue;
 
 private void Start()
 {
     light = GetComponent<Light>();
 }
 
 // Update is called once per frame
 void Update()
 {   
     if(value <= Mathf.Epsilon && SwipeInput.swipedUp)
         targetValue = 1;
     else if(value >= 1 - Mathf.Epsilon && SwipeInput.swipedDown)
         targetValue = 0;
 
     value = Mathf.MoveTowards(value, targetValue, Time.deltaTime / Duration);
     light.color = Gradient.Evaluate(value);
 }
That worked, thanks! But how could i do the same thing for intensity?
If the intensity must change at the same time as the color:
  light.color = Gradient.Evaluate(value);
  light.intensity = $$anonymous$$athf.Lerp($$anonymous$$Intensity, maxIntensity, value);
If you want the two properties must be independent:
  public Gradient Gradient ; // Provide the colors in the inspector
  public float Duration;
  private Light light;
  private float colorValue;
  private float colorTargetValue;
  private float intensityValue;
  private float intensityTargetValue;
  
  private void Start()
  {
      light = GetComponent<Light>();
  }
  
  // Update is called once per frame
  void Update()
  {   
      UpdateColor();
      UpdateIntensity();
  }
  private void UpdateColor()
  {
      if(colorValue <= $$anonymous$$athf.Epsilon && SwipeInput.swipedUp)
          colorTargetValue = 1;
      else if(colorValue >= 1 - $$anonymous$$athf.Epsilon && SwipeInput.swipedDown)
          colorTargetValue = 0;
          
      colorValue = $$anonymous$$athf.$$anonymous$$oveTowards(colorValue, colorTargetValue, Time.deltaTime / Duration);
      light.color = Gradient.Evaluate(colorValue);
  }
  private void UpdateIntensity()
  {
      if(intensityValue <= $$anonymous$$athf.Epsilon && SwipeInput.swipedRight)
          intensityTargetValue = 1;
      else if(intensityValue >= 1 - $$anonymous$$athf.Epsilon && SwipeInput.swipedLeft)
          intensityTargetValue = 0;
      intensityValue = $$anonymous$$athf.$$anonymous$$oveTowards(intensityValue, intensityTargetValue, Time.deltaTime / Duration);
      light.intensity = $$anonymous$$athf.Lerp($$anonymous$$Intensity, maxIntensity, intensityValue);
  }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                