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 sebeisdrache · Feb 10, 2015 at 11:44 PM · directiondirectional lightrelativelocalspaceglobalspace

Set a sun light (Day Night Cycle)

Hello,

I want to make a day/night cycle by using the planetay rotaion. Now I have a Problem, the yewllow line in the Screenshot is the Direction to the Star for a Local directional Light

I have trouble with the correct direction of the Local Sun Light.

The Local Directional Light has the script:

 using UnityEngine;
 using System.Collections;
 
 public class LocalSun : MonoBehaviour
 {
 
     public GameObject star;
     public GameObject skyBoxCam;
     public LayerMask layer;
 
     Light localSun;
 
     void Start ()
     {
         localSun = light;
     }
 
     void Update ()
     {
         Quaternion starDirection = Quaternion.LookRotation(star.transform.position - skyBoxCam.transform.position);
         Vector3 v3LocalStarDirection = starDirection * Vector3.forward;
 
         Debug.DrawRay(skyBoxCam.transform.position, v3LocalStarDirection, Color.yellow);
 
         Debug.DrawRay(skyBoxCam.transform.position, skyBoxCam.transform.forward, Color.blue);
         Debug.DrawRay(skyBoxCam.transform.position, skyBoxCam.transform.right, Color.red);
         Debug.DrawRay(skyBoxCam.transform.position, skyBoxCam.transform.up, Color.green);
 
         Vector3 v3Relative = skyBoxCam.transform.parent.transform.TransformDirection(v3LocalStarDirection);
 
         Ray ray = new Ray(skyBoxCam.transform.position, starDirection * Vector3.forward);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit, 10000f, layer.value))
         {
             if(hit.transform.light)
             {
                 light.enabled = true;
             }
             else
             {
                 light.enabled = false;
             }
         }
 
 
         Debug.DrawRay(transform.position, v3Relative, Color.white);
         transform.rotation = Quaternion.LookRotation(v3Relative);
 
     }
 }
 


The skyBoxCam object is parented to an object to algin it to the planet surface in an easier way.

I think the problem is the way I try to Copy the Direction to the Sun:

Vector3 v3Relative = skyBoxCam.transform.parent.transform.TransformDirection(v3LocalStarDirection);

The Sunlight points in a wrong Direction and can't go under the horizon if I Rotate the Planet (but the Yellow line[direction to the sun] is under the horizon).

I'm sorry for the odd english, I'm not a native speaker.

alt text

problem.jpg (278.0 kB)
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 sebeisdrache · Feb 11, 2015 at 11:38 AM 0
Share

Now I can fix this by using a relativ roation offset.

 using UnityEngine;
 using System.Collections;
 
 public class LocalSun : $$anonymous$$onoBehaviour
 {
 
     public GameObject star;
     public GameObject skyBoxCam;
     public Layer$$anonymous$$ask layer;
 
     Light localSun;
 
     void Start ()
     {
         localSun = light;
     }
 
     void Update ()
     {
         Quaternion starDirection = Quaternion.LookRotation(star.transform.position - skyBoxCam.transform.position);
         /*
         Vector3 v3LocalStarDirection = starDirection * Vector3.forward;
         Debug.DrawRay(skyBoxCam.transform.position, v3LocalStarDirection, Color.yellow);
         Debug.DrawRay(skyBoxCam.transform.position, skyBoxCam.transform.forward, Color.blue);
         Debug.DrawRay(skyBoxCam.transform.position, skyBoxCam.transform.right, Color.red);
         Debug.DrawRay(skyBoxCam.transform.position, skyBoxCam.transform.up, Color.green);
         */
 
         Ray ray = new Ray(skyBoxCam.transform.position, starDirection * Vector3.forward);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit, 10000f, layer.value))
         {
             if(hit.transform.light)
             {
                 light.enabled = true;
             }
             else
             {
                 light.enabled = false;
             }
         }
 
         Quaternion offset = Quaternion.Inverse(skyBoxCam.transform.parent.rotation) * starDirection;
         Debug.DrawRay(transform.position, offset * Vector3.forward, Color.white);
         transform.rotation = offset;
         transform.Rotate(0,180,0);
 
     }
     
 }
 

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

How does GetRelativeVector work? 1 Answer

Simple Transform question, using Quaternion.LookRotation 0 Answers

Changing scale of unit 0 Answers

Global and Local(newbie) 1 Answer

A way to convert Relative direction to world? 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