• 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
Question by Aladine · Oct 06, 2013 at 05:06 PM · timespeed

set the speed in respect of time

hello everyone, am trying to make a day-night cycle by lerping the color of a the material from blue to black and also i have an emptyObject that contain 2 spheres (sun & moon) sun is in (0,10,0) and moon is in (0,-10,0) (that emptyObject is a child of the camera) now the thing is that i managed to make it work for the color by setting the lerp time(t) to this : t += Time.deltaTime / duration;

and i thought that the correct way to do this for the sun and moon object is to make Speed = Distance / duration ;

but it never worked, am sure that this is the right formula but the variable "Distance" and "Duration" am not sure that am entering them right, notice that in order to make a full day cycle, the object should rotate 180 degree in it's Z axis (maybe this could help)

thank you

Comment

People who like this

0 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 robertbu · Oct 06, 2013 at 05:21 PM 0
Share

Seeing your code would be helpful to answer this question.

avatar image meat5000 ♦ · Oct 06, 2013 at 05:26 PM 0
Share

If you are talking about arcing a Sun across the sky, you can use Slerp.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Aladine · Oct 06, 2013 at 07:53 PM

@meat5000 thank you that save me a lot of time, the sun cycle is not working perfectly, but the color lerp is not perfectly synchronized

code : sun Slerp :

 using UnityEngine;
 using System.Collections;
 
 public class sunSet : MonoBehaviour
 {
     //sunrise position
     public Transform sunrise;
     //sunset position
     public Transform sunset;
     //total time  
     public float journeyTime = 1.0f ;
     //counter
     private float startTime;
     //materials 
     public Material sun, moon;
     
     void Start ()
     {
         //init
         startTime = Time.time;
         gameObject.renderer.material = sun;
         
     }
 
     void Update ()
     {
         journey ();
         
     }
 
     void journey ()
     {
         //Slerp code from unity script doc 
         Vector3 center = (sunrise.position + sunset.position) * 0.5F;
         center -= new Vector3 (0, 1, 0);
         Vector3 riseRelCenter = sunrise.position - center;
         Vector3 setRelCenter = sunset.position - center;
         float fracComplete = (Time.time - startTime) / journeyTime;
         transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
         transform.position += center;
         
         //loop the cycle and change material 
         if (transform.position == sunset.position) {
             transform.position = sunrise.position;
             startTime = Time.time;
             if (gameObject.renderer.material.name == "sun (Instance)") {
                 gameObject.renderer.material = moon;
             } else {
                 gameObject.renderer.material = sun;
             }
             
         }
     }
 }


SkyColor lerp :

 using UnityEngine;
 using System.Collections;
 
 public class SkyTrans : MonoBehaviour
 {
     //day & night colors
     public Color day = new Color (0.01f, 0.4f, 0.8f);
     public Color night = new Color (0.01f, 0.01f, 0.02f);
     //main color
     public Color skyColor ;
     //total time 
     public float journeyTime;
     //lerp speed 
     public float lerpSpeed ;
     //checking if it's day or night 
     bool isDay ;
     //instance of the sun object 
     sunSet sun;
 
     void Start ()
     {
         
         //init 
         sun = GameObject.Find ("sun").GetComponent<sunSet> ();
         lerpSpeed = 0; 
         journeyTime = sun.journeyTime;
         skyColor = day;
         isDay = true;
     }
     
     void Update ()
     {
         //setting the color to skyColor 
         gameObject.renderer.material.color = skyColor;
     
         
         if (lerpSpeed < 1) {
             lerpSpeed += Time.deltaTime / journeyTime;
         }
         
         //if it's day then lerp to night 
         if (isDay) {
             skyColor = Color.Lerp (day, night, lerpSpeed / 2);
         }
         //if it's night then lerp to day 
         else {
             skyColor = Color.Lerp (night, day, lerpSpeed / 2);
         }
         //day or night 
         if (skyColor == day) {
             isDay = true;
             lerpSpeed = 0;
         } else if (skyColor == night) {
             isDay = false;
             lerpSpeed = 0;
         }
         
     }    
 }


thank you

Comment

People who like this

0 Show 0 · 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

Answer by aldonaletto · Oct 06, 2013 at 08:16 PM

You should post your script! Anyway, I suppose that both things must cycle from day to night and night to day - am I right? If so, the empty could be Rotate'd at the right speed, while the color should Lerp from blue to black during the sunset, then back to blue during the dawn. My suggestion is to control both with the same variable in order to avoid loss of synchronicity over time. Basically, the code could be something like this:

 public var sunMoon: Transform; // drag here the empty that contains sun and moon
 public var duration: float = 3600; // day duration in seconds (1 hour, in this example)
 public var transitionTime: float = 0.2; // transition duration control
 public var nightColor = Color.black;
 public var dayColor = Color.blue;
 
 private var curTime: float = 0;
 
 function Update(){
   curTime += Time.deltaTime; // update curTime
   var sunAngle: float = 360*curTime/duration; // calculate rotation angle
   sunMoon.eulerAngles = Vector3(0,0,sunAngle); // set rotation angle about Z
   // transition goes 0->1 when sunAngle near to zero, 1->0 when around 180
   var transition: float = Mathf.Sin(sunAngle*Mathf.Deg2Rad)/transitionTime + 0.5;
   var skyColor: Color = Color.Lerp(nightColor, dayColor, transition);
   // apply skyColor to the appropriate object
 }
Comment

People who like this

0 Show 1 · 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 Aladine · Oct 06, 2013 at 08:40 PM 0
Share

thank you, but as you may noticed am not using the rotation method anymore, the Slerp function work perfectly the problem now is with the color transition

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Increase speed every 5 seconds 1 Answer

[Solved] Reverse animation help 2 Answers

Speed = 11 meters /s plane is 110 meters should take me 10 seconds but it dosn't 0 Answers

Problem with time !! help me 1 Answer

Increasing speed for a few seconds 2 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