Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rewriting my day/night cycle when this happened.

Discussion in 'Scripting' started by TonyNowak, May 19, 2013.

  1. TonyNowak

    TonyNowak

    Joined:
    May 10, 2010
    Posts:
    316
    http://pastebin.com/h1Vrwba9

    Says I am declaring the var t twice (which I am), what I'm not understanding is t is a clamp between 0 and 1 so I can Lerp the colors together. Is there a way to do this without this error, how could I fix it?
     
    Last edited: May 21, 2013
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Don't declare it again but re-use it.
     
  3. TonyNowak

    TonyNowak

    Joined:
    May 10, 2010
    Posts:
    316
    So have var t once, then just t? I tried that and it doesn't know what "t" is.
     
  4. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    uhm .. here ya go

    Code (csharp):
    1.  
    2. //Define variables.
    3.  
    4. var startColor : Color;
    5. var endColor : Color;
    6. var duration: float = 15; // duration in seconds
    7.  
    8. var st : float = 0.1f;
    9.  
    10. function Update () {
    11.  
    12.       if (Input.GetKeyDown("space")){
    13.         st = 0;
    14.     }
    15.     //renderer.material.color = Color.Lerp(startColor, endColor, t);
    16.     st = Mathf.PingPong (Time.time, duration) / duration;
    17.     RenderSettings.ambientLight = Color.Lerp (startColor, endColor, st);
    18.     if (st < 1){ // while t below the end limit...
    19.     // increment it at the desired rate every update:
    20.         st += Time.deltaTime/duration;
    21.     }
    22. }
    23.  
    24.  
     
  5. TonyNowak

    TonyNowak

    Joined:
    May 10, 2010
    Posts:
    316
    Well, that's not exactly what I was looking for. Where did you get input from? Where are all the other variables as well that actually make it a cycle? Does anyone else have a solution.
     
  6. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Code (csharp):
    1. function Update () {
    2.     if (Input.GetKeyDown(KeyCode.Space))
    3.     {
    4.         var t:float = Time.time;
    5.         Debug.Log(t);
    6.     }
    7.    
    8.     if (Input.GetKeyDown(KeyCode.Q))
    9.     {
    10.         t = 1.1f;
    11.         Debug.Log(t);
    12.     }
    13.  }
    or

    Code (csharp):
    1. function Update () {
    2.     var t:float = 0.0f;
    3.     if (Input.GetKeyDown(KeyCode.Space))
    4.     {
    5.         t = Time.time;
    6.         Debug.Log(t);
    7.     }
    8.    
    9.     if (Input.GetKeyDown(KeyCode.Q))
    10.     {
    11.         t = 1.1f;
    12.         Debug.Log(t);
    13.     }
    14. }
    both work
     
  7. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    I thought having working code was what we wanted here? It works and it's something i want to try out quickly in my scene :D
     
  8. TonyNowak

    TonyNowak

    Joined:
    May 10, 2010
    Posts:
    316
    Is there a way it could work with what I have now, I'm not the best coder, more of a hobby if anything.
     
  9. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    You do know that your script can never work this way ...
    You are yielding in the update method which will give errors.
     
  10. TonyNowak

    TonyNowak

    Joined:
    May 10, 2010
    Posts:
    316
  11. AstralProjection

    AstralProjection

    Joined:
    Feb 12, 2013
    Posts:
    11
    Also, you have two booleans for IsNight and IsDay which is error prone. You only need one of them. When IsDay == false then it is night.
     
  12. TonyNowak

    TonyNowak

    Joined:
    May 10, 2010
    Posts:
    316
    Check the new pastebin link.