Clock

I have a clock hand in my game that I want to rotate from
z = 15 to z = -195
over the course of an in-game day.
What do you think would make it point to
z = 15 when time = dayStartTime
and
z = -195 when time = dayEndTime
?
###Time System###
Currently, I’m storing time as an integer, 600 = 6:00 AM and 2600 = 2:00 AM, but I’ve run into a bit of an issue because I want to keep these time references simple, so I can reference times like 9:30 AM as 930, but that means I have to make 659 flip to 700 and that breaks smooth increments (such as one for rotating a clock hand from the start of the day to the end of the day). Is there a way to keep my time as an integer for reference?
Any help in either topic is appreciated so much! Thank you for reading!
I hope you appreciate it, it’s not the best but if you have the right skills you can perfect it… It would have been better to create functions to get the time parameters (hour, minute, seconds) but I didn’t have the head for now … maybe one of these days I’ll fix ti. The only flaw for me was getting the notation change, otherwise it works…
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace Gavn.UI
{
public class WorldClock: MonoBehaviour
{
[Header("Clock Parameters")]
[SerializeField] private int REAL_SECONDS_PER_INGAME_DAY = 5; //Speed of game seconds - (Change at your own risk!)
[SerializeField] public bool FORMAT_TIME; //Use the 24-hour or 12-hour format
[Header("Game Parameters")]
[SerializeField] private bool DAY_OR_NIGHT; //DAY = true : NIGHT = false//
[Header("Time Parameters")]
[SerializeField] private int hours = 12;
[SerializeField] private int minutes = 0;
[SerializeField] private float seconds = 0;
[Space(10)]
[SerializeField] public string clock;
[Space(10)]
[SerializeField] private int format;
private float timer;
private string notation;
private bool meridian;
private float hoursDegrees = 360 / 12;
private float minutesDegrees = 360 / 60;
private float secondsDegrees = 360 / 60;
[Space(10)]
[SerializeField] private Transform _handClockHours;
[SerializeField] private Transform _handClockMinutes;
[SerializeField] private Transform _handClockSeconds;
private void Awake()
{
_handClockHours = GameObject.Find("HandClockHours").GetComponent<Transform>();
_handClockMinutes = GameObject.Find("HandClockMinutes").GetComponent<Transform>();
_handClockSeconds = GameObject.Find("HandClockSeconds").GetComponent<Transform>();
}
private void Start()
{
_handClockHours.eulerAngles = new Vector3(0, 0, -hours * hoursDegrees);
_handClockMinutes.eulerAngles = new Vector3(0, 0, -minutes * minutesDegrees);
_handClockSeconds.eulerAngles = new Vector3(0, 0, -seconds * secondsDegrees);
}
private void FixedUpdate()
{
HandleTime();
NumberSystem();
RotateHandClock();
TimeInGame();
}
private void HandleTime()
{
timer += Time.fixedDeltaTime * REAL_SECONDS_PER_INGAME_DAY;
seconds = (int)timer;
notation = DAY_OR_NIGHT ? "AM" : "PM";
}
private void NumberSystem()
{
//Set the clock's format
if (FORMAT_TIME)
{
format = 12;
if (hours > 12)
{
hours = 1;
}
}
else
{
format = 24;
if (hours >= 12)
{
hours = 0;
}
}
//Set right hours
if (minutes >= 60)
{
minutes = 0;
hours++;
}
//Set right minutes plus reinitialize seconds
if (seconds >= 60)
{
timer = 0;
minutes++;
}
//Meridian notation change ** Some fixes need to be made (incorrect function, probably glitches)
if(FORMAT_TIME && timer < 0.1f)
{
if (hours == 12)
DAY_OR_NIGHT = !DAY_OR_NIGHT;
}
}
private void RotateHandClock()
{
_handClockHours.rotation = Quaternion.Euler(0, 0, -hours * hoursDegrees);
_handClockMinutes.rotation = Quaternion.Euler(0, 0, -minutes * minutesDegrees);
_handClockSeconds.rotation = Quaternion.Euler(0, 0, -seconds * secondsDegrees);
}
private void TimeInGame()
{
if(FORMAT_TIME)
clock = (Mathf.Floor(hours).ToString("00") + ":" + Mathf.Floor(minutes).ToString("00") + notation);
else
clock = (Mathf.Floor(hours).ToString("00") + ":" + Mathf.Floor(minutes).ToString("00"));
}
}
}
I’ve got the beginning of an answer! The only problem is that it takes until 42:00 for the clock hand to spin to its final value instead of the desired 26:00.
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace Gavn.UI
{
public class WorldClock : MonoBehaviour
{
[SerializeField] private int dayStartHour = 6;
[SerializeField] private int dayEndHour = 26;
[SerializeField] private int dayStartMinute = 0;
[SerializeField] private int dayEndMinute = 0;
[SerializeField] private Transform clockHand;
[SerializeField] private TextMeshProUGUI timeText;
public int currentGameHour = 0;
public int currentGameMinute = 0;
private int dayDeltaHours;
public float secondsPerDay = 900;
private float minClockHandRotation = 15f; // Pointed all the way to the left
private float maxClockHandRotation = -195f; // Pointed all the way to the right
private float clockHandDeltaAngle;
private float clockHandRotationPerInGameMinute;
private float timer = 0;
private float delayAmount;
private object currentGameHourString;
private object currentGameMinuteString;
private object currentTimeString;
private void Start()
{
dayDeltaHours = dayEndHour - dayStartHour;
delayAmount = (secondsPerDay) / (dayDeltaHours * 60);
clockHandDeltaAngle = maxClockHandRotation - minClockHandRotation;
clockHandRotationPerInGameMinute = dayDeltaHours / clockHandDeltaAngle;
currentGameHour = dayStartHour;
}
private void Update()
{
timer += Time.deltaTime;
if(timer >= delayAmount)
{
timer = 0f;
if (currentGameMinute < 59)
{
currentGameMinute ++;
}
else
{
currentGameMinute = 0;
if (currentGameHour < 23)
{
currentGameHour ++;
}
else
{
currentGameHour = 0;
}
}
currentTimeString = currentGameHour.ToString("00") + ":" + currentGameMinute.ToString("00");
//Debug.Log(currentTimeString + " with a delay of: " + delayAmount);
timeText.text = (string)currentTimeString;
clockHand.eulerAngles += new Vector3(0, 0, clockHandRotationPerInGameMinute);
//Debug.Log("Adding " + clockHandRotationPerInGameMinute + " to clock hand z axis rotation");
}
}
}
}