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 /
avatar image
0
Question by tonny · Feb 23, 2012 at 03:43 PM · lightcolourcycleambient

Change light.color over time, repeat

Hi,

I've already found some bits of code here but that still doesn't answer my question. I'm trying to create a day/night cycle. I have 4 colours for each day time. So morning, noon, dusk, night. They are stored in separate variables.

I want the light colour to lerp from one to another in certain amount of time per colour. Can someone write a bit of code and hopefully describe WHY and WHAT'S happening? :)

tl;dr I want to cycle between 4 light colours to create a day/night cycle. Also, how do I loop this behaviour?

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 · Feb 27, 2012 at 04:59 PM 0
Share

You could look at Fade for a starting point, specifically the third Colors function. The only thing you'd have to add is a certain amount of time for each color.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CanOfColliders · Feb 27, 2012 at 02:10 PM

hm, still at work and can't actually test this right now to write it, but take a look at http://www.youtube.com/watch?v=zxIyZzieFqE this covers a day / night cycle setup, including time and looping. It's not actually blending colors but uses a moving sun/moon & blending skyboxes but hope that helps

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
avatar image
0

Answer by Rs · Sep 25, 2012 at 05:16 PM

Hello, here's my example. I do not use lerp here but if you need that you can substitute the Mathf.Sin with any functions you want. Some code might be redundant to you since I included some timing optimization. The light is updated every t you specify in UPDATE_INTERVAL. The rest should be straight forward. Hope it helps.

Note: you might need more precise functions to map the sun colour on time. These were just OK for my purposes.


 using UnityEngine;
 using System.Collections;
 
 public class DayTimer : MonoBehaviour {
     
     // Update every UPDATE_INTERVAL seconds
     private const int SECONDS_IN_A_DAY = 86400;
     
     // variables to do timing on the update (optimization)
     private const float UPDATE_INTERVAL = 1f;
     private float lastUpdateTime = 0f;
     
     private System.DateTime now = System.DateTime.Now;
     public bool manualControl = false;
     public int currentDayTime = 0;
     
     // Light that should be controlled by the DayTimer
     public Light controlledLight01 = null;
 
     // Use this for initialization
     void Start () {
         lastUpdateTime = Time.time;
     }
     
     void Update () {
         if(manualControl) {
             
         } else {
             now = System.DateTime.Now;
             currentDayTime = System.Convert.ToInt16(now.Hour)*3600 + System.Convert.ToInt16(now.Minute)*60 + System.Convert.ToInt16(now.Second);
         }
         
         // Update the light every UPDATE_INTERVAL seconds
         if(Time.time > lastUpdateTime + UPDATE_INTERVAL) {
             if(controlledLight01!=null) { // If we have a light
                 Light l = controlledLight01.GetComponent<Light>(); // Get the Light component
                 // Mathf.Sin doesn't convert automatically to float so the given values need to be converted manually
                 float currentDayTimeF = System.Convert.ToSingle(currentDayTime);
                 float secondsInADayF = System.Convert.ToSingle(SECONDS_IN_A_DAY);
                 Color c = new Color();
                 c.r=Mathf.Sin(currentDayTimeF/secondsInADayF * 3.14159f);
                 c.g=Mathf.Sin(currentDayTimeF/secondsInADayF * 3.14159f);
                 c.b=Mathf.Sin(currentDayTimeF/secondsInADayF * 3.14159f) * 0.3f;
                 c.a=1f;
                 l.color = c;
                 lastUpdateTime = Time.time;
             }
         }
         
     }
 }
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

8 People are following this question.

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

Related Questions

Ambient light and lightmapping 1 Answer

How to remove elliptical color bands on textures 1 Answer

How to slowly change the colour of a directional light object 3 Answers

Day Night Cycle sun fade 1 Answer

Independent Ambient Light Shader 0 Answers


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