Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by _eternal · Mar 26, 2015 at 05:22 PM · colortimelerpgradientdatetime

Smooth gradient between colours?

I'm making a day/night cycle that's tied to the user's real time (obtained via DateTime.Now).

For testing purposes, my sky graphics are tied to the seconds count from DateTime. A full day is 60 seconds. I created a gradient with colours set at various intervals and I'm calling it from Update() via gradient.Evaluate(gradientCounter), where gradientCounter is the current time in seconds divided by 60.

However, this means that the sky colour is only updated every second, not every frame. To fix this, I tried adding a Lerp to the bottom of Update(), like this:

Color.Lerp(gradient.Evaluate(gradientCounter), gradient.Evaluate(nextGradientCounter), Time.deltaTime)

nextGradientCounter is simply the current time in seconds, plus one, divided by 60.

But this attempt did not work, and the colour is still only changing every second. Does anyone know why the Lerp failed or if there's a better solution?

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 karl_ · Mar 26, 2015 at 05:30 PM

I think it's because DateTime.Second is an integer - so dividing by 60 is just going to return 0. Here's an example using the DateTime.Millisecond property to lerp from one end of the gradient to the other over a 1 second period:

 using UnityEngine;
 using System.Collections;
 
 public class GradientTest : MonoBehaviour {
 
     public Gradient gradient;
 
     void OnGUI()
     {
         System.DateTime date = System.DateTime.Now;
 
         GUI.color = gradient.Evaluate( date.Millisecond / 1000f );
 
         GUILayout.Label("Hello, world!");
     }
 }
 
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 _eternal · Mar 26, 2015 at 05:53 PM 0
Share

That works, but how would I convert it to a one-$$anonymous$$ute cycle? I can't just change 1000f to 60000f because the millisecond count gets reset every second. And I can't do date.Second / 60f because then the colour only changes every second.

avatar image karl_ · Mar 26, 2015 at 06:00 PM 1
Share

If you want it to be one cycle per $$anonymous$$ute would just do gradient.Evaluate( (date.Second * 1000f + date.$$anonymous$$illisecond) / (60f * 1000f) ). So you take the seconds, convert that to milliseconds (seconds * 1000), add the current second's milliseconds, then divide by the total milliseconds in a $$anonymous$$ute (60 * 1000) to normalize that value.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

change Mesh.colors over time help? 2 Answers

Controlling duration of Color.Lerp in seconds 2 Answers

Color lerp once? 2 Answers

Gradient Background With Random Color Lerp 0 Answers

Time.deltaTime making color lerp appear fast, but won't reach 1 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