- Home /
 
How do you get the 24 Hour Time from rotation on the x axis?
I made a day-night script, and wish to make a method in it which is called every frame to get the 24 hour time based on it's rotation, but I tried for about a week and had no luck. (Also does anyone know how to add stars?) Here's my code so far:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DayNight : MonoBehaviour {
 
     public float MinutesInOneDay = 1.0f;
 
     public Text DisplayMins;
     public Text ActualMins;
 
     public static string CurrentDayTime="00:00";
 
     float timer;
     float turnSpeed;
 
     // Use this for initialization
     void Start () {
         timer = (transform.rotation.x / 60.0f) * 360;
     }
     
     // Update is called once per frame
     void Update () {
         UpdateLights ();
 
         turnSpeed = 360.0f / (MinutesInOneDay * 60.0f) * Time.deltaTime;
         transform.RotateAround(transform.position, transform.right, (-1)*turnSpeed);
         checkTime ();
     }
 
     void UpdateLights(){
         Light l = GetComponent<Light> ();
         if (isNight ()) {
             if (l.intensity > 0.0f) {
                 l.intensity -= 0.05f;
             }
         } else {
             if(l.intensity < 1.0f) {
                 l.intensity += 0.05f;
             }
         }
     }
 
     bool isNight() {
         bool c = false;
         if (timer > (MinutesInOneDay * 60.0f)) {
             c = true;
         }
         return c;
     }
 
     void checkTime(){
         float rot=float.Parse(transform.eulerAngles.x.ToString());
         //print (rot.ToString());
 
         int currMinutes = (Mathf.RoundToInt(rot/0.25f));
         print ("currMinutes: "+(currMinutes).ToString());
         int currHours = 25-(int.Parse(Mathf.Floor(rot/15).ToString()));
         int currDisplayMinutes=currMinutes-(currHours*60);
 
         CurrentDayTime = currHours.ToString() + " : " + currDisplayMinutes.ToString();
         DisplayMins.text= "Time: " + CurrentDayTime;
         ActualMins.text="currMinutes: " + currMinutes.ToString();
     }
 }
 
              Answer by GearBreak · Jan 29, 2018 at 04:23 PM
I am not going to answer this question directly by editing your own code, but there's a free asset on the unity assetstore for day/night cycles wich has hours, minutes and am/pm as public variable. I hope it still helps you: https://assetstore.unity.com/packages/tools/particles-effects/day-and-night-cycle-with-scattering-and-moving-clouds-27024
[it's still working in unity 2017.3.0f3 64bit]
Your answer
 
             Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Script is Flipping Out! 2 Answers
Conversion of seconds to hours and minutes 1 Answer
Distribute terrain in zones 3 Answers