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 TobiasJohansson · Apr 21, 2014 at 10:49 AM · programming

Rotate sun during set of time

I have searched and found no answers that work for what I'm trying to do. So now I really need help.

I want to make a day and night cycle in my game. For this I want the sun to move certain degrees during different timesets of the day. For example during sunrise the sun should move 60 degrees, and during day it should move 120 degrees. And I want the sun to do this during the time spent in each section so when I reach the next time set (for example from sunrise to day)it should have reached the final degrees.

So if my sunrise is 10 seconds I want the sun to move 60 degrees in 10 seconds. And when I reach 10 seconds my timeset will turn into day and then I want the sun to move 120 degrees in 20 seconds. So the sun always moves, and it moves the amount of degrees it should during each timeset. However I have not been able to do this.

This is the last function of like a million I tried without luck.

     IEnumerator RotateSun(Vector3 fDegrees, float SecondsDuringTimeSet)
     {
         //lSunLight.transform.eulerAngles = new Vector3(fDegrees, 0, 0);
         Quaternion quaFromPosition = lSunLight.transform.rotation;
         Quaternion quaToPostion = Quaternion.Euler(lSunLight.transform.eulerAngles + fDegrees);
 
         for (float t = 0.0f; t < 1; t += Time.deltaTime / SecondsDuringTimeSet)
         {
             lSunLight.transform.rotation = Quaternion.Lerp(quaFromPosition, quaToPostion, t);
             yield return null;
         }
     }


Could someone please help me with this? I've been stuck forever with this now.

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 robertbu · Apr 21, 2014 at 03:44 PM 1
Share

I might have done some things differently, but I don't see anything wrong in your code. The above code should rotate the specified object the degrees specified in the time specified. Is this not what is happening, or is your issue with combining the three rotations, or...?

avatar image TobiasJohansson · Apr 21, 2014 at 04:57 PM 0
Share

Yeah, the code worked, it was just extremely heavy, dropped my FPS to about 10. Figured that might be since I accidently called it every frame though... $$anonymous$$issed that for some reason. :D

But I would be interested in how you would do it? I don't feel like a StartCoroutine is the best either, just could not come up with another idea.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TobiasJohansson · Apr 22, 2014 at 10:48 AM

Mm, I kind of got the code to work now. But still have one problem. It does not only rotate arond the x-axis for some reason. After a while I see that both y and z also changes and this screws up my day and night since it then after a while makes the y and z higher and the sun starts rotating more and more on the side, which change the time it takes to get to a certain degree.

 // Call and use the Coroutine            
 fMoveSun = 20.0f;
 StartCoroutine(RotateSun(fMoveSun, fConvertSunriseToGTSeconds));    
 
 // The Coroutine
 IEnumerator RotateSun(float fDegrees, float SecondsDuringTimeSet)
     {
         Quaternion quaFromPosition = lSunLight.transform.rotation;
         Quaternion quaToPostion = Quaternion.Euler(lSunLight.transform.eulerAngles + new Vector3(fDegrees, 0, 0));
 
         for (float t = 0.0f; t < 1; t += Time.deltaTime / SecondsDuringTimeSet)
         {
             lSunLight.transform.rotation = Quaternion.Lerp(quaFromPosition, quaToPostion, t);
             yield return null;
         }
     }

Does anyone know how I can stop this? I thought I already did that by setting the y and z to 0 for each rotation but apperently that does not work.

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 FlaflaTwo · Apr 22, 2014 at 11:20 AM 0
Share

Take a look at the docs for Quaternion.Euler:

Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

So if you want to change the X rotation of the element, you need to set your axis as either Y or Z.

avatar image
0

Answer by FlaflaTwo · Apr 21, 2014 at 04:56 PM

This code will work if the sun object you are referring to is simply a directional light. If you have something like a flare attached to it (in other words, you want the sun to physically move across the sky) you need to use Transform.RotateAround(). Transform.Rotate only affects the local rotation of the object - so it simply rotates around its center of mass. Thus, in this code your sun is rotating (so you would see it if the sun has a directional light), but the position of the sun remains the same.

Comment
Add comment · 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

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Need help fixing procedural generation bug 1 Answer

Change color on button click | Visual Scripting BOLT 0 Answers

How do I change the rotation of an object after setting transform.up? 0 Answers

Inheritance in C# and Unity3d 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