Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by LadyWulff · Apr 07, 2017 at 06:59 PM · scripting problemlighting2d gamelerpdaycycle

Attaching Light lerp to in-game clock

Hey all, I've been working on a 2d day/night cycle for about a week now, but I've hit a roadblock. I've been trying to get the light to turn different shades in sync with the in-game clock I have working, but no matter what I do, it will not do what I want it to. I've searched through the Unity Answers section for hours and hours, and I've about exhausted every thread I've found. The light either just lerps through morning to midday and then stops, or it flickers through at rapid speed, or just pops in the third color in the array, or something weird like that.

I want to find a way to sync them so that the coloring of the light will always match what time the clock says, even when I speed up the cycle for debug purposes. It would also help to be able to jump it forward, like if a cutscene plays out and takes one in game hour.

I'm still somewhat new to Unity, so any help I can get would be greatly appreciated.

This is the code I have to the clock: public class DayNightCycle : MonoBehaviour {

     private int dayLength;
     private int morningStart;
     private int noonStart;
     private int eveningStart;
     private int nightStart;
     private int midnight;
     private int currentTime;
 
     public Color morningShade;
     public Color noonShade;
     public Color eveningShade;
     public Color nightShade;
 
     public float cycleSpeed;
     private bool isDay;
     private Vector3 sunPos;
     public Light sun;
     public GameObject earth;
 
     // Use this for initialization
     void Start () {
         dayLength = 1440; //in-game minutes
         morningStart = 360; //6am
         noonStart = 720; //12pm
         eveningStart = 1080; //6pm
         nightStart = 1200; //8pm
         midnight = 1440; //12am
         currentTime = 360;
         StartCoroutine (TimeofDay ());
         earth = gameObject.transform.parent.gameObject;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (currentTime > 0 && currentTime < morningStart) {
             isDay = false;
         } else if (currentTime >= morningStart && currentTime < noonStart) {
             //isDay = true;
         } else if (currentTime >= nightStart && currentTime < dayLength) {
             isDay = false;
         } else if (currentTime >= dayLength) {
             currentTime = 0;
         }
         float currentTimeF = currentTime;
         float dayLengthF = dayLength;
         earth.transform.eulerAngles = new Vector3 (0, 0, (-(currentTimeF / dayLengthF) * 360) + 90);
         sun.color = 
     }
 
     IEnumerator TimeofDay(){
         while (true) {
             currentTime += 1;
             int hours = Mathf.RoundToInt (currentTime / 60);
             int minutes = currentTime % 60;
             Debug.Log (hours + ":" + minutes);
             yield return new WaitForSeconds (1F / cycleSpeed);
         }
     }
 }
 
 
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
0

Answer by UnityCoach · Apr 07, 2017 at 07:37 PM

You could simplify this a bit using a Gradient instead.

 public Gradient dayLight;
 
 void Update ()
 {
     sun.color = dayLight.Evaluate (currentTime); // must be normalised between 0 and 1
 }
Comment
Add comment · Show 4 · 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 LadyWulff · Apr 07, 2017 at 08:47 PM 0
Share

Using a gradient did cross my $$anonymous$$d... But what do you mean normalize is between 1 and 0? And also, how would the gradient account for the use of 4 light colors? And would it be able to loop through the cycle properly?

avatar image UnityCoach LadyWulff · Apr 07, 2017 at 09:29 PM 0
Share

The value you pass to Gradient.Evaluate must be between 0 and 1.

So, you'll have to figure out the math so that 0 = midnight, 0.25 = 6am, 0.5 = 12pm and 0.75 = 6pm.

You can then add as many colours to the gradient as you like. If you want to handle more than just one light, you could have several gradients, one for the sun, one for the ambient for example.

You could also use the alpha for the intensity, though it can't go passed 1, so I would rather use an AnimationCurve along for the intensity, as it works the same way as a Gradient.

avatar image LadyWulff UnityCoach · Apr 10, 2017 at 09:24 PM 0
Share

Sorry if this is a little nocroposty. I'm still having issues. I see what you're suggesting, it's just implementing it now that's the problem. I can't get the gradient to increment. I believe the currentTime variable need to be somehow changed to accommodate a 0-1 scale. Is there a way to do that without messing everything up in the normal clock? Or do I need to create a whole new cycle that runs simultaneously to the clock, and how would those sync up?

I know. I'm full of a thousand questions here. :P

Show more comments

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

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

Related Questions

Animation frezzing on first few frames android 0 Answers

Main Menu Help 2 Answers

Can someone help me with the movement of the ufo from the ufo tutorial 1 Answer

I can't get my scene to darken 0 Answers

How to change rigidbody type correctly? Angry Birds Game 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