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 AT-Brackeys · May 26, 2013 at 09:29 PM · rotationdirectioncyclenightday

Object rotation shifting

So I'm trying to expand on an old day/night cycle I found and it is working really well. I have a sun animate across the sky and a directional light that rotates. Everything is controlled by a slider which I have looping whenever it exceeds max (1). The problem is that the light (and sun) sometimes doesn't cycle 360 degrees but stop halfway and then starts going in the opposite direction. It sometimes does this multiple times in a row. The light intensity still works but the rotating of the directional light doesn't. I hope you can help me out :) Here is the code:

 var slider : float;
 var slider2 : float;
 var Hour : float;
 private var Tod: float;
 
 var sun: Light;
 
 var speed = 50;
 
 var NightFogColor : Color;
 var DuskFogColor : Color;
 var MorningFogColor : Color;
 var MiddayFogColor : Color;
 
 var NightAmbientLight : Color;
 var DuskAmbientLight : Color;
 var MorningAmbientLight : Color;
 var MiddayAmbientLight : Color;
 
 var NightTint : Color;
 var DuskTint : Color;
 var MorningTint : Color;
 var MiddayTint : Color;
 
 var SkyBoxMaterial1 : Material;
 var SkyBoxMaterial2 : Material;
 
 var SunNight : Color;
 var SunDay : Color;
 
 
 
 
 function OnGUI () {
 
 if(slider >= 1.0)
 {
     slider = 0;
 }
 
 slider= GUI.HorizontalSlider( Rect(20,20,200,30), slider, 0,1.0);
 Hour= slider*24;
 Tod= slider2*24;
 sun.transform.localEulerAngles.x= (slider*360)-90;
 slider = slider +Time.deltaTime/speed;
 sun.color = Color.Lerp (SunNight, SunDay, slider*2);
 if(slider<0.5){
 slider2= slider;
 }
 if(slider>0.5){
 slider2= (1-slider);
 }
 sun.intensity = (slider2-0.2)*2;
 
 
 if(Tod<4){
 //it is Night
 RenderSettings.skybox=SkyBoxMaterial1;
 RenderSettings.skybox.SetFloat("_Blend", 0);
 SkyBoxMaterial1.SetColor ("_Tint", NightTint);
 RenderSettings.ambientLight = NightAmbientLight;
 RenderSettings.fogColor = NightFogColor;
 }
 if(Tod>4&&Tod<6){
 RenderSettings.skybox=SkyBoxMaterial1;
 RenderSettings.skybox.SetFloat("_Blend", 0);
 RenderSettings.skybox.SetFloat("_Blend", (Tod/2)-2);
 SkyBoxMaterial1.SetColor ("_Tint", Color.Lerp (NightTint, DuskTint, (Tod/2)-2) );
 RenderSettings.ambientLight = Color.Lerp (NightAmbientLight, DuskAmbientLight, (Tod/2)-2);
 RenderSettings.fogColor = Color.Lerp (NightFogColor,DuskFogColor, (Tod/2)-2);
 //it is Dusk
 
 }
 if(Tod>6&&Tod<8){
 RenderSettings.skybox=SkyBoxMaterial2;
 RenderSettings.skybox.SetFloat("_Blend", 0);
 RenderSettings.skybox.SetFloat("_Blend", (Tod/2)-3);
 SkyBoxMaterial2.SetColor ("_Tint", Color.Lerp (DuskTint,MorningTint,  (Tod/2)-3) );
 RenderSettings.ambientLight = Color.Lerp (DuskAmbientLight, MorningAmbientLight, (Tod/2)-3);
 RenderSettings.fogColor = Color.Lerp (DuskFogColor,MorningFogColor, (Tod/2)-3);
 //it is Morning
 
 }
 if(Tod>8&&Tod<10){
 RenderSettings.ambientLight = MiddayAmbientLight;
 RenderSettings.skybox=SkyBoxMaterial2;
 RenderSettings.skybox.SetFloat("_Blend", 1);
 SkyBoxMaterial2.SetColor ("_Tint", Color.Lerp (MorningTint,MiddayTint,  (Tod/2)-4) );
 RenderSettings.ambientLight = Color.Lerp (MorningAmbientLight, MiddayAmbientLight, (Tod/2)-4);
 RenderSettings.fogColor = Color.Lerp (MorningFogColor,MiddayFogColor, (Tod/2)-4);
 
 //it is getting Midday
 
 }
 }
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
1
Best Answer

Answer by aldonaletto · May 26, 2013 at 09:48 PM

I suspect that the problem is caused by this line:

 sun.transform.localEulerAngles.x= (slider*360)-90;

You should never assign to single components of eulerAngles or localEulerAngles - always assign the 3 components at once. Supposing that the components Y and Z are zero, change this line to:

 sun.transform.localEulerAngles = Vector3((slider*360)-90, 0, 0);

If Y and Z aren't both zero, save their initial values at Start:

 private var euler: Vector3;

 function Start(){
     euler = sun.transform.localEulerAngles;
 }

   ...
   // modify X in variable euler and update localEulerAngles:
   euler.x = (slider*360)-90;
   sun.transform.localEulerAngles = euler;
   ...

Comment
Add comment · 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 AT-Brackeys · May 26, 2013 at 10:45 PM 0
Share

Thank you very much, this was just what I need, it solved my problem :)

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

14 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

Related Questions

Photon Day Night Cycle Unsynced 1 Answer

Blend two lightmaps for day/night cycle? (terrain) 0 Answers

Day Night Cycle Script Not Working 3 Answers

Day/Nights cycle 2 Answers

Day Night cycle question 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