Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by THEpancakes · Oct 07, 2012 at 10:32 AM · colortimelerpsmoothseconds

Controlling duration of Color.Lerp in seconds

Hi everyone,

I';ve asked a question similar to this recently, but it just got me wondering... How can you control the duration of Color.Lerp in seconds?

Take this code for example:

 var startColor : Color;
 var endColor : Color;

 function Update() {
     renderer.material.color = Color.Lerp(startColor, endColor, Time.time);
 }

How could modify this so I can explicitely control the duration of the transition in SECONDS?

Thanks in advance. :)

Comment
Add comment · Show 1
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 Eric5h5 · Oct 07, 2012 at 03:41 PM 0
Share

Not sure why you're asking since you already saw my answer in your other question: http://wiki.unity3d.com/index.php/Fade

3 Replies

· Add your reply
  • Sort: 
avatar image
21
Best Answer

Answer by aldonaletto · Oct 07, 2012 at 11:28 AM

The Lerp examples in the docs are terribly bad: using Time.time as the control variable limits things to the first second your game is playing! Ok, you could simply use Time.time/duration instead of just Time.time (duration in seconds), but this would not allow a new sequence without restarting the game.
That's a simple example that allows duration control and sequence restarting:

 var startColor : Color;
 var endColor : Color;
 var duration: float = 5; // duration in seconds
 
 private var t: float = 0; // lerp control variable
     
 function Update() {
   renderer.material.color = Color.Lerp(startColor, endColor, t);
   if (t < 1){ // while t below the end limit...
     // increment it at the desired rate every update:
     t += Time.deltaTime/duration;
   }
 }

You can restart the Lerp sequence at any time just assigning 0 to t. A more elaborated version could do it when you press space, for instance:

 var startColor : Color;
 var endColor : Color;
 var duration: float = 5; // duration in seconds
 
 private var t: float = 0; // lerp control variable
     
 function Update() {
   if (Input.GetKeyDown("space")){
     t = 0;
   }
   renderer.material.color = Color.Lerp(startColor, endColor, t);
   if (t < 1){ // while t below the end limit...
     // increment it at the desired rate every update:
     t += Time.deltaTime/duration;
   }
 }
Comment
Add comment · Show 6 · 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 THEpancakes · Oct 07, 2012 at 01:02 PM 0
Share

That doesn't seem to work, it does the same as the code I was using before... Duration is supposed to be in seconds, but when I enter five (or any other number) into the variable box, the Lerp still goes just as fast. :( What am I doing wrong?

avatar image aldonaletto · Oct 07, 2012 at 05:58 PM 1
Share

Are you trying the last script? $$anonymous$$odifying duration in the Inspector and pressing space should Lerp from startColor to endColor in the duration specified. Create a new blank script, paste the code above and attach it to the object - but make sure the old version isn't also attached to it!

avatar image clunk47 · Oct 16, 2013 at 03:42 AM 0
Share

This will also work, just set the size of your array in the inspector, then set the colors.

 using UnityEngine;
 using System.Collections;
 
 public class LerpColor : $$anonymous$$onoBehaviour 
 {
     public Color[] c;
     Color color;
     float t = 0;
     float i = 0.025f;
     bool change = false;
     
     void Awake()
     {
         color = c[0];    
     }
     
     void Update()
     {
         renderer.material.color = Color.Lerp(c[0], c[1], t);
         if(!change)
             t+=i;
         else
             t-=i;
         if(t>=1)
             change = true;
         if(t<=0)
             change = false;
     }
 }
avatar image Sami_Pal · Aug 30, 2016 at 07:48 AM 0
Share

@aldonaletto It works for me, but the transition between the two colors is kinda in a gradient way, so I put the duration to 1, it switch color with gradient but it two fast, I want the colors changes without any gradient effect and kinda like a quick reverse but with adjustable duration. Any suggestion.

avatar image Kouznetsov · Jan 21, 2019 at 10:26 PM 1
Share

Still relevant after seven years.

Show more comments
avatar image
10

Answer by Shadowphax · Jul 24, 2014 at 08:19 AM

Hi there.

I've not personally used the Color.lerp but I presume it works the same as any lerping function since the 3rd parameter is in the range 0 - 1.

I would recommend using Coroutines for this type of thing. Here is an example:

 float duration = 5; // This will be your time in seconds.
 float smoothness = 0.02f; // This will determine the smoothness of the lerp. Smaller values are smoother. Really it's the time between updates.
 Color currentColor = Color.white; // This is the state of the color in the current interpolation.
 
 void Start()
 {
     StartCoroutine("LerpColor");
 }
 
 IEnumerator LerpColor()
 {
     float progress = 0; //This float will serve as the 3rd parameter of the lerp function.
     float increment = smoothness/duration; //The amount of change to apply.

     while(progress < 1)
     {
        currentColor = Color.Lerp(Color.red, Color.blue, progress);
        progress += increment;
        yield return new WaitForSeconds(smoothness);
     }
 }

I haven't tested the code but I think it's good enough to demonstrate. If you don't know coroutines I would strongly recommend learning about them. It's very useful for animating things between 2 points. Not just colors but anything. It sure as hell beats keeping track of time variables in update loops.

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 hawken · Feb 02, 2016 at 07:15 AM 1
Share

Almost right ;)

Never seen it done like this, very interesting.

Smoothness/duration should be the other way around, wait for should be increment.

avatar image fillevoss hawken · Sep 27, 2016 at 01:20 PM 0
Share

Shadowphax's code was correct the way he wrote it, atleast for me.

avatar image
3

Answer by Fattie · Oct 10, 2018 at 02:33 PM

Slowly cycle the background color....



It's remarkably easy to cycle through all the colors of the rainbow:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ColorCycle : MonoBehaviour {
 
        // put this script on your camera
        // it's great for a Canvas for your UI
 
     private Camera cam;
     private float cycleSeconds = 100f; // set to say 0.5f to test
     
     void Awake() {
         
         cam = GetComponent<Camera>();
     }
     
     void Update() {
         
         cam.backgroundColor = Color.HSVToRGB(
             Mathf.Repeat(Time.time / cycleSeconds, 1f),
             0.3f,     // set to a pleasing value. 0f to 1f
             0.7f      // set to a pleasing value. 0f to 1f
         );
     }
 }




That's it.

For an object ... with proper offset from the editor starting color look, etc.

 using UnityEngine;
 public class ObjectColorCycle : MonoBehaviour
 {
     public Renderer colorMe;
 
     void Update()
     {
         Colorz();
     }
 
     float origHue = 0.5f;
     float origSat = 0.5f;
     float origVal = 0.5f;
 
     void Start()
     {
         Color c = colorMe.material.color;
         Color.RGBToHSV(c, out origHue, out origSat, out origVal);
     }
 
     private float cycleSeconds = 100f; // as you wish
 
     void Colorz()
     {
         float h = Mathf.Repeat(origHue + Time.timeSinceLevelLoad / cycleSeconds, 1f);
         colorMe.material.color = Color.HSVToRGB(h, origSat, origVal);
     }
 }




That's easy fortunately.

Comment
Add comment · 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

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

24 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

Related Questions

Color lerp once? 2 Answers

Time.deltaTime making color lerp appear fast, but won't reach 1 1 Answer

make an event occur after so many seconds? 1 Answer

Mathf.Lerp happens instantly 2 Answers

Smooth gradient between colours? 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