Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SunLight rotation

Discussion in 'Scripting' started by rokstar234, Jul 31, 2011.

  1. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    hello everyone, im having some difficulty with making my sun( a directional light with a flare) move to make the Day and night effect, im trying to make it orbit over the terrain by using a parent empty in the center and making that move the sun, and the sun rotates according to the time of day. but they aren't moving at all or at least not what the inspector says, heres the code is there anything wrong?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SunScript : MonoBehaviour {
    6.     public float minutes = 0;
    7.     public float hours = 0;
    8.     public float Day = 1;
    9.     public int month = 1;
    10.     public int Year = 2467;
    11.     public Transform myTransform;
    12.     void Awake()
    13.     {
    14.         myTransform = transform;
    15.      float rot = myTransform.rotation.x;
    16.         rot = 0;
    17.     }
    18.    
    19.    
    20.    
    21.     // Use this for initialization
    22.     void Start () {
    23.          
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.         minutes = minutes + 1 * Time.deltaTime;
    29.         if (minutes > 60)
    30.         {
    31.             hours = hours + 1;
    32.             minutes = 1.0f;
    33.         }
    34.         if (hours == 48)
    35.         {
    36.             Day = Day + 1;
    37.             hours = 1;
    38.         }
    39.         if (Day == 31)
    40.         {
    41.             month = +1;
    42.             Day = 1;
    43.         }
    44.         sunPOS();
    45.         SUNLIGHT();
    46.        
    47.        
    48.     }
    49.    
    50.     public void sunPOS()
    51.     {
    52.         float Posistion = myTransform.rotation.x;
    53.         Posistion = ((hours * 60) + minutes);
    54.     }
    55.     public void SUNLIGHT()
    56.     {
    57.         GameObject SunLight = GameObject.Find("Sun");
    58.  
    59.         float Light = SunLight.transform.rotation.x + ((hours * 60) + minutes);
    60.      
    61.     }
    62. }
    63.  
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    You code is kind of hard to read, but i assuming this is the line you are assuming moves the sun

    float Light = SunLight.transform.rotation.x + ((hours * 60) + minutes);

    however it doesn't it just sets that value into the new variable light. I use unityscript so I am not that practiced with c# but it would like

    SunLight.transform.rotation.x = //inser what you want it it to equal.

    I don't get how you are calculating at angle but remember if you are working in degrees to use the euler version of rotation.

    I also don't get what the sunPOS function is meant to do.
     
  3. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    SunPos rotates the parent object to make the sun orbit, sunlight rotates the sun itself to change he brightness of the sun. doing as you suggest throws back an error telling me that SunLight.transform.rotation.x can't be changed because its not a variable. thanks anyway
     
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249

    well you can use transform.rotate instead then?


    Why don't you just attach the script to the light so you don't have to use the find. That is pretty processor intensive and should be avoided if possible.


    You don't need to change the position of the directional light, but it doesn't matter what position it is in the scene as the light only has direction(you can see this my moving a directional light around the scene and if you don't rotate it everything will stay the same).


    None the less your problem is clearly you are storing the number in a variable, rather than changing the value of the actual object.
     
  5. Divinux

    Divinux

    Joined:
    Jul 5, 2011
    Posts:
    205
    Code (csharp):
    1.  
    2. var sun : Transform;
    3.  
    4. if (second > 60)
    5.     {
    6.         second = 0;
    7.         minute++;
    8.         sun.RotateAround(Vector3.up, Vector3.right, 0.25);
    9.         if (minute > 60)
    10.         {...and so on}
    11.  
    works quite well for me
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    erm... mathematically....

    Code (csharp):
    1.  
    2. var offset : float=-180;
    3. var sunPosition : float=(hour * 3600 + minute * 60 + second) / 86400 * 360 + offset;
    4. var rotation = sunRotator.localEulerAngles;
    5. rotation.x=sunPosition;
    6. sunRotator.localEulerAngles=rotation;
    7.  
     
  7. rokstar234

    rokstar234

    Joined:
    Mar 29, 2011
    Posts:
    94
    thanks, thats my Day/night system done and dusted, thank's you very much.