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

Rotate around Position by Degrees

Discussion in 'Scripting' started by DirtyNative, Nov 22, 2014.

  1. DirtyNative

    DirtyNative

    Joined:
    Jun 3, 2014
    Posts:
    8
    Hi Community,

    what i want to achieve is a Sun rotating around my Player (for a God Ray Effect). I've got a TimeSystem Script that calcutes the Time (really Simple)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class TimeSystem {
    5.  
    6.     public static float TimeSpeed = 1f;
    7.     public static float Minute;
    8.     public static float Hour;
    9.     public static float ActualTime;
    10.  
    11.     public static void Update () {
    12.         Minute += Time.deltaTime * TimeSpeed;
    13.        
    14.         if (Minute >= 60) {
    15.             Minute -= 60;
    16.             Hour++;
    17.         }
    18.        
    19.         if (Hour >= 24){
    20.             Hour -= 24;
    21.         }
    22.        
    23.         ActualTime = ((Hour * 60) + Minute) / (24 * 60);
    24.     }
    25. }
    26.  
    And i had a basic Rotation for my Sun (Rotation around an axis)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class DayNightSystem {
    5.  
    6.     public static GameObject SunLight;
    7.  
    8.     public static float SunMaxIntensity = 0.69f;
    9.     public static float SunMinIntensity = 0f;
    10.  
    11.     public static float rotation;
    12.     private static float rotationY;
    13.     private static float rotationZ;
    14.  
    15.     private static Color DayLightColor;
    16.     private static Color DawnLightColor;
    17.     private static Color NightLightColor;
    18.  
    19.     private static float beginNightToDawn = 5;
    20.     private static float beginDawnToDay = 6;
    21.     private static float endDawnToDay = 8;
    22.  
    23.     private static float beginDayToDawn = 16;
    24.     private static float beginDawnToNight = 17;
    25.     private static float endDawnToNight = 18;
    26.  
    27.     public static GameObject ObjectToRotateAround;
    28.  
    29.     public static void Start(){
    30.         rotationY = SunLight.transform.eulerAngles.y;
    31.         rotationZ = SunLight.transform.eulerAngles.z;
    32.  
    33.         DayLightColor = Color.white;
    34.         DawnLightColor = new Color(1f,0.4f,0f);
    35.         NightLightColor = new Color (0, 0, 0);
    36.     }
    37.  
    38.     public static void Update () {
    39.         UpdateLightRotation ();
    40.         UpdateLightColor ();
    41.     }
    42.  
    43.     public static void UpdateLightRotation(){
    44.         rotation = 360 * TimeSystem.ActualTime - 90;
    45.        
    46.         if (rotation >= 360) {
    47.             rotation -= 360;      
    48.         }
    49.         SunLight.transform.eulerAngles = new Vector3(rotation, rotationY, rotationZ);
    50.         transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    51.         SunLight.transform.RotateAround (ObjectToRotateAround.transform.position , Vector3.right, angle);
    52.     }
    53.  
    54.     public static void UpdateLightColor(){
    55.         if (TimeSystem.Hour >= beginNightToDawn && TimeSystem.Hour < beginDawnToDay) {
    56.             float percent = ((TimeSystem.Hour - beginNightToDawn) * 60 + TimeSystem.Minute) / ((beginDawnToDay - beginNightToDawn) * 60);
    57.             SunLight.GetComponent<Light>().color = Color.Lerp (NightLightColor, DawnLightColor, percent);
    58.         }
    59.         else if (TimeSystem.Hour >= beginDawnToDay && TimeSystem.Hour < endDawnToDay) {
    60.             float percent = ((TimeSystem.Hour - beginDawnToDay) * 60 + TimeSystem.Minute) / ((endDawnToDay - beginDawnToDay) * 60);
    61.             SunLight.GetComponent<Light>().color = Color.Lerp (DawnLightColor, DayLightColor, percent);
    62.         }
    63.         else if (TimeSystem.Hour >= beginDayToDawn && TimeSystem.Hour < beginDawnToNight) {
    64.             float percent = ((TimeSystem.Hour - beginDayToDawn) * 60 + TimeSystem.Minute) / ((beginDawnToNight - beginDayToDawn) * 60);
    65.             SunLight.GetComponent<Light>().color = Color.Lerp (DayLightColor, DawnLightColor, percent);
    66.         }
    67.         else if (TimeSystem.Hour >= beginDawnToNight && TimeSystem.Hour < endDawnToNight) {
    68.             float percent = ((TimeSystem.Hour - beginDawnToNight) * 60 + TimeSystem.Minute) / ((endDawnToNight - beginDawnToNight) * 60);
    69.             SunLight.GetComponent<Light>().color = Color.Lerp (DawnLightColor, NightLightColor, percent);
    70.         }
    71.  
    72.     }
    73. }
    74.  
    As you can see the Sun was rotated by the actual Time.
    But now i want the Sun rotating, like mentioned above, around a Gameobject (especially around the Player). But i don't get it to work. Any Suggestions?

    Greetings
     
  2. Diukrone

    Diukrone

    Joined:
    Nov 20, 2017
    Posts:
    10
    Put your gameobject on rotate around parameters from sun. I think you need create a new condition, another function.