• 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 Struders · Aug 10, 2014 at 07:33 AM · daycycle

Slowing down day/night cycle when sun is near horizens

Hello all,

I'm using a simple transform to rotate the position of the sun at a constant speed right now. What I would really like is for the night to go quickly and for dawn/dust to linger for a while.

I just have no idea how I would do this. Any thoughts?

Comment
Add comment · Show 1
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 JustFun · Aug 10, 2014 at 10:55 AM 2
Share

Create new animation for sun rotation angle and adjust its keyframes to provide rotation speed as you need.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 10, 2014 at 01:43 PM

@JustFun's suggestion of using an animations is likely the easiest solution. But as a programmer, programming is my hammer and everything looks like a nail. So here is an approach using some code. I'm going to assume that you've implemented your cycle by having an empty game object at the center and by placing your sun as an outlying child. So you would rotate the empty game object to rotate the sun across the sky.

I sometimes have trouble getting my head wrapped around problems like these, so I standardize things. First I write my code so that movement or cycling is mapped into the values between 0.0 and 1.0. Usually what I'm remapping is time. So for the above rotation, I might write this coroutine:

 function DayNight(time : float) {
     var timer : float;
     
     while (true) {
         timer += Time.deltaTime / time;
         timer = timer % 1.0;
         var a = Map(timer) * 360.0;
         transform.rotation = Quaternion.AngleAxis(a, Vector3.forward);
         yield;
     }
 }

And then a linear Map() function is:

 function Map(var f) {
     return f;
 }

And you would call it in Start() for a 30 second day/night cycle:

  DayNight(30.0);

This is lot of code for something that can be done in a single line in Update(), but now I can easily map the input through some sort of curve or calculation. So to use a Cosine function to slow the rotation at near 0 and 180 degrees, I can rewrite map to:

 function Map(f : float) : float{
     if (f <= 0.5) 
         var ff = (Mathf.Cos(f * Mathf.PI * 2.0 - Mathf.PI) + 1.0) / 4.0;
     else 
         ff = (Mathf.Cos((f - 0.5) * Mathf.PI * 2.0 - Mathf.PI) + 1.0) / 4.0 + 0.5;
     return ff;
 }

And if I want more control, I can write a version of the Map function that takes an AnimationCurve.

 var ac : AnimationCurve;
 
 function Map(f : float) : float {
     return Mathf.Clamp01(ac.Evaluate(f));
 }

And the curve might look like:

alt text

And since an AnimationCurve is such a flexible solution, that brings us back full circle to the suggestion that an Animation might be the simplest solution, and this problem isn't a nail at all.


ac1.png (20.6 kB)
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 JustFun · Aug 10, 2014 at 03:42 PM 0
Share

Wow! Thanks! Things like this definitely must be in bookmarks :)

avatar image
0

Answer by Struders · Aug 10, 2014 at 02:15 PM

Thanks guys for taking the time to reply. Very in depth!

I thought about animation but it's something I've not really tackled beyond the basics so far so I kind of avoided it.

I think I'll look into the animation solution.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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 avatar image

Related Questions

Real-time day/night system breaks after noon 1 Answer

fading between skyboxes 1 Answer

Realtime daylight cycle? 1 Answer

Single lightmap with day/night cycle 0 Answers

Passing variables' values between two or more scripts 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