• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 /
  • Help Room /
avatar image
0
Question by 14ercooper · Dec 16, 2015 at 12:32 AM · sundaycycle

Cannot make script to rotate sun

I tried to make a script to have the sun in my scene travel through the sky, and I the sun wouldn't move, nor will the light change. What am I doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class SunSpinner : MonoBehaviour {
     // How many second between day/night ticks (3600 per day-night cycle)
     [SerializeField] private float daySpeed = 0.05f;
     // Days that have passed
     [SerializeField] private float daysPassed = 0f;
     // Holds the sun's transform
     [SerializeField] private Transform sunTransform;
 
     // When was day/night last updated
     private float lastTimeTick = 0f;
     // Complete rotation of the sun
     private Quaternion solarRotation = new Quaternion(90f, 0f, 0f, 1f);
     
     // Update is called once per frame
     void Update () {
         if (Time.realtimeSinceStartup - lastTimeTick >= daySpeed) {
             // Rotates the sun by 0.01 degrees
             solarRotation.x = (solarRotation.y + 0.1f);
 
             //Sees if a day has passed
             if (solarRotation.x >= 360f) {
                 solarRotation.x = 0f;
                 daysPassed ++;
             }
             
             //Rotate the sun very slightly on the X axis
             solarRotation.z = solarRotation.x + 0.00001f;
 
             // Prepares for next rotation and updates the game object
             sunTransform.rotation = solarRotation;
             lastTimeTick = Time.realtimeSinceStartup;
 
         }
     }
 
     public void Reset () {
         lastTimeTick = Time.realtimeSinceStartup;
     }
 }
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 LazyElephant · Dec 16, 2015 at 12:58 AM 0
Share

I don't see where you're initializing your sunTransform. Where are you assigning your sun object to this?

avatar image 14ercooper LazyElephant · Dec 16, 2015 at 01:20 AM 0
Share

In the editor, I put a reference to it there

2 Replies

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

Answer by LazyElephant · Dec 16, 2015 at 02:03 AM

Long story short, changing the x, y, z, w variables in the quaternion doesn't do what I think you were expecting. Changing those variables directly generally isn't a good idea unless you really know the math behind the scenes, because it's not the same as simply adjusting the x/y/z rotations. You can make your script work by changing your solarRotation variable from a Quaternion to a Vector3 and by changing line 33 to sunTransform.rotation = Quaternion.Euler(solarRotation);

Comment
Add comment · Show 7 · 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 14ercooper · Dec 16, 2015 at 03:29 AM 0
Share

The sun still stays just a touch above the horizon, and then doesn't move.

avatar image LazyElephant 14ercooper · Dec 16, 2015 at 03:33 AM 0
Share

For my test, I just placed it on a directional light to make sure that it was rotating and the lighting would change. The reason for your sun not moving is probably specific to your setup. How do you have your sun set up in your scene?

avatar image 14ercooper · Dec 16, 2015 at 03:41 AM 0
Share

My sun is the one that comes defaulted to the scene, with only that script attached.

avatar image LazyElephant 14ercooper · Dec 16, 2015 at 03:57 AM 0
Share

oh, it's my fault. I forgot to mention that I also changed line 21 to solarRotation.x = (solarRotation.x + 0.1f);

In the script you have, you're setting it to solarRotation.y + 0.1f. But since solarRotation.y isn't changing anywhere in your script, you're just setting the solarRotation.x to the same value every time.

One other thing you'll see when it's running is that the rotation value for x will actually be counting down instead of up and it will automatically wrap around to 360 when it reaches 0. So you'll need to adjust your day tracking code.

avatar image 14ercooper · Dec 16, 2015 at 03:55 AM 0
Share

Also, does not work with a fresh directional light

avatar image maccabbe · Dec 16, 2015 at 04:00 AM 0
Share

It sounds like your "sun" is just the directional light. Its position should not have any effect on your game.

However if you wanted it to see it "move" or "travel through the sky" then you have to change its position not its rotation. This can be done directly using transform.position or indirectly in a number of ways including transform.RotateAround

http://docs.unity3d.com/ScriptReference/Transform-position.html

http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

avatar image 14ercooper · Dec 16, 2015 at 04:04 AM 0
Share

Thanks! That worked.

avatar image
2

Answer by Eno-Khaon · Dec 16, 2015 at 04:53 AM

As @LazyElephant stated, the problem is misuse of Quaternions, but what may seem like the simplest solution does not actually function as easily as it could. Euler angles may seem correct at first, but the way Unity handles them comes second to directly-Quaternion-based functions.

One possible way of accounting for this is, rather than using the sun's position to determine the time, use the time to determine the sun's position. For instance:

 // C#
 
 [SerializeField] private float dayCycleTime = 180.0f; // 3600 * 0.05
 
 [SerializeField] private float rotationAngleOffset = -90; // Starting rotation from (0, 0, 0), where a time of "0" is midnight, offset rotation by 90 degrees
 
 [SerializeField] private Vector3 sunRotationAxis = Vector3.forward; // Rise in the east, set in the west
 
 private float sunSpeedScale; // 360 degree rotation occurs in "dayCycleTime" seconds.
 // This scale will accommodate this using the formula (360 / dayCycleTime)
 
 void Start()
 {
     // Correct for dayCycleTime <= 0
     if(dayCycleTime <= 0)
     {
         dayCycleTime = 0.0001f;
     }
     sunSpeedScale = 360.0f / dayCycleTime;
 }
 
 void Update()
 {
     // use of Time.realtimeSinceStartup may be replaced in order to choose when time starts, or to be able to pause time as desired
     sunTransform.rotation = Quaternion.AngleAxis((Time.realtimeSinceStartup * sunSpeedScale) + rotationAngleOffset, sunRotationAxis);
 }

This version should not find itself caught in the data-type conversion between euler angles and Quaternion rotations which prevent high values from being utilized.

Comment
Add comment · Show 2 · 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 LazyElephant · Dec 16, 2015 at 04:59 AM 0
Share

This is a good solution. I'll have to remember it. +1

avatar image 14ercooper · Dec 16, 2015 at 01:08 PM 0
Share

This is a valid solution, as well.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

How do you reflect a directional light Sun in Unity5 water? 2 Answers

Why Is My Survival Shooter Scene Still in Daytime? Why is my Zombunny Not Trying to Shoot the Player? 0 Answers

Switching from light to another still keeps area with old light 1 Answer

Get procedural skybox's colors 0 Answers

Can't get "Sun size" "Sun" "Atmosphere thickness" 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges