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 BHS · Mar 08, 2012 at 07:29 PM · inputtimetransition

Transition based off day length speed? ( day/night system)

Hi,

Edit: Written in java.

I'm working on a day/night system and I'd like the transition time to be based off the daylength input. The whole system is in sync with the daylength speed so if the daylength speed is set at 10 it's a 10 second day and so forth.

How can I have the timeOfDay transition according to the daylength speed? I need it to be consistently transitioned at whatever speed the days are. Maybe a transition time similar to Skyrim's based off a 24 minute day, then if the speed is set to a 1 minute day then the transitions will be the same, but just sped up. Hopefully I explained it well enough.

How can I do this? Here's some of what I've coded you can base it off that.

 Hour = calculate1*24;
 timeOfDay = calculate2*24;
 calculate1 = calculate1 +Time.deltaTime/dayLength;

  //Calculates night
 if(timeOfDay<4){

 RenderSettings.ambientLight = NightAmbientLight;

 cameraThing.camera.backgroundColor = Color.Lerp (backgroundNightAmbientLight, 
     backgroundNightAmbientLight, timeOfDay);
 }

Comment
Add comment · Show 2
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 Jacob-Aldridge · Mar 08, 2012 at 08:39 PM 0
Share

In your Lerp, you are using the backgroundNightAmbientLight twice. Did you intend that?

avatar image BHS · Mar 08, 2012 at 09:20 PM 0
Share

Yes, because it's transitioning, unless that's incorrect. I'm still relatively new to program$$anonymous$$g so I could be mistaken. I've tested it and it all changes properly from day to dusk to night to dawn. I just need a way of smoothly transitioning it based on the timeofday not instantly like its doing now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jacob-Aldridge · Mar 08, 2012 at 10:23 PM

This is what I came up with. Sorry if it's a bit huge, but I thought it might be useful to make it decent. I tested it and it seems to work fine. Let me know if it doesn't work correctly.

 using UnityEngine;
 using System.Collections;
 using System;

 public class TimeOfDay : MonoBehaviour
 {
    public float timePerDay = 1140.0f; // In seconds
    public float sunrise = 6.0f; // In hours
    public float sunset = 17.0f; // In hours
    public float hoursPerDay = 24.0f; // In hours

    public Color dayAmbientColor;
    public Color dayBackgroundColor;
    public Color nightAmbientColor;
    public Color nightBackgroundColor;

    public float currentTime = 0.0f;

    private float NightDuration
    {
        get { return timePerDay - RelativeSunset + RelativeSunrise; }
    }

    private float DayDuration
    {
        get { return RelativeSunset - RelativeSunrise; }
    }

    private float RelativeSunrise
    {
        get { return timePerDay * (sunrise / hoursPerDay); }
    }

    private float RelativeSunset
    {
        get { return timePerDay * (sunset / hoursPerDay); }
    }

    private float Midday
    {
        get { return (DayDuration * 0.5f) + RelativeSunrise; }
    }

    private float Midnight
    {
        get { return Mathf.PingPong((NightDuration * 0.5f) + RelativeSunset, timePerDay); }
    }

    // Use this for initialization
    public void Start ()
    {
 
    }
 
    // Update is called once per frame
    public void Update ()
    {
       currentTime += Time.deltaTime;

       // If we have reached the end of day start the cycle over
       if (currentTime >= timePerDay)
           currentTime = currentTime - timePerDay; // Keep the extra time

       Color currentAmbientColor = Color.black;
       Color currentBackgroundColor = Color.black;

       float midday = Midday;
       float midnight = Midnight;
       float sampleTime = 0.0f;

       float minutesPastMidnight = (currentTime > midnight && currentTime < timePerDay) ? timePerDay - currentTime : timePerDay - midnight + currentTime;
       if (currentTime > midday && currentTime <= midnight)
       {
           sampleTime = (currentTime - midday) / DayDuration;
           Debug.Log("Sample Day: " + sampleTime);
       }
       else if (minutesPastMidnight >= 0)
       {
           sampleTime = 1.0f - (minutesPastMidnight / NightDuration);
       }

       currentAmbientColor = Color.Lerp(dayAmbientColor, nightAmbientColor, sampleTime);
       currentBackgroundColor = Color.Lerp(dayBackgroundColor, nightBackgroundColor, sampleTime);

       RenderSettings.ambientLight = currentAmbientColor;
       Camera.main.backgroundColor = currentBackgroundColor;
    }
 }
Comment
Add comment · Show 3 · 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 BHS · Mar 08, 2012 at 10:46 PM 0
Share

$$anonymous$$y apologies, I should have mentioned it was written in java. I'm not sure how to convert it, but thank you for this though.

avatar image Berenger · Mar 08, 2012 at 10:46 PM 0
Share

I just added that script on the wiki, if your interested.

avatar image BHS · Mar 08, 2012 at 11:22 PM 0
Share

Thanks, but I already have the time counter down base off of the day length. $$anonymous$$ine includes $$anonymous$$tues, hours, days, months, and years. Thanks though.

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

6 People are following this question.

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

Related Questions

Does Unity provide a means to determine the time since the last user interaction? 2 Answers

An easy way to add level transitions? 1 Answer

How can I make it so that inputs only function once per second? 2 Answers

How can I use multiple GetButtonDown keys simultaneously? 2 Answers

Hold down button for in trigger 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