Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
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);
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bogdan003 · Apr 07, 2020 at 03:31 PM 0
Share

That worked, thanks! But how could i do the same thing for intensity?

avatar image Hellium Bogdan003 · Apr 07, 2020 at 03:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why change the light color so fast? 2 Answers

Make the cube have the same color as the background every 3 seconds 0 Answers

Quad Gradual Colour Change 1 Answer

Instantiating multiple sprites and assigning different colors for each 1 Answer

Change Fog Color Smoothly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges