- Home /
Clock script with custom time not working
I've been trying to create a script for the clock for which you can enter start and end times manually, but for some reason it doesn't seem to work. I'm very new at this so I understand the fact that there may be a lot of logical mistakes in the code, but if someone could help out, it would be very much appreciated. With the given code, the second hand twitches and doesn't want to move forward. I tried implementing a timer into the script so it adjusts to the start and end time that was set up. I hope someone can make some sense of this. Here's the code:
using System.Collections;
using UnityEngine;
public class Clock : MonoBehaviour
{
#region Variables
#region References
public Transform hourHand, minuteHand, secondHand;
#endregion
#region Settings
[Header("Settings")]
public float rotationTime;
[Header("Starting Time")]
[Range(0, 11)] public int startHour;
[Range(0, 59)] public int startMinute;
[Range(0, 59)] public int startSecond;
[Header("Ending Time")]
[Range(0, 11)] public int endHour;
[Range(0, 59)] public int endMinute;
[Range(0, 59)] public int endSecond;
#endregion
#region DEBUG
private float hourHandStartAngle, minuteHandStartAngle, secondHandStartAngle;
private float hourHandAngleOffset, minuteHandAngleOffset, secondHandAngleOffset = 6f;
private int currentHour, currentMinute, currentSecond;
private float currentTime;
private float secondTimer = 1f;
private float minuteTimer = 60f;
private float hourTimer = 360f;
private float finalStartHour, finalStartMinute, finalStartSecond;
private Quaternion currentHourRotation, currentMinuteRotation, currentSecondRotation;
private Quaternion newHourRotation, newMinuteRotation, newSecondRotation;
private float hourHandStartTime, hourHandEndTime, minuteHandStartTime, minuteHandEndTime, secondHandStartTime, secondHandEndTime;
private float hourHandRotationTransitionPercentage, minuteHandRotationTransitionPercentage, secondHandRotationTransitionPercentage;
#endregion
#endregion
#region Built-in Methods
void Start()
{
finalStartHour = (float)startHour;
finalStartMinute = (float)startMinute;
finalStartSecond = (float)startSecond;
hourHandStartAngle = -360 * (finalStartHour / 12);
minuteHandStartAngle = -360 * (finalStartMinute / 60);
secondHandStartAngle = -360 * (finalStartSecond / 60);
hourHand.localRotation = Quaternion.Euler(0, hourHandStartAngle, 0);
minuteHand.localRotation = Quaternion.Euler(0, minuteHandStartAngle, 0);
secondHand.localRotation = Quaternion.Euler(0, secondHandStartAngle, 0);
currentHour = startHour;
currentMinute = startMinute;
currentSecond = startSecond;
StartCoroutine(ClockTimer());
}
void Update()
{
currentTime += Time.deltaTime;
}
#endregion
#region Coroutines
private IEnumerator ClockTimer()
{
while(currentHour != endHour || currentMinute != endMinute || currentSecond != endSecond)
{
if(currentTime > secondTimer)
{
if(RotateSecondHand() != null)
{
StopCoroutine(RotateSecondHand());
}
StartCoroutine(RotateSecondHand());
secondTimer += 1f;
currentSecond += 1;
if(currentSecond == 60)
{
currentSecond = 0;
}
}
if(currentTime > minuteTimer)
{
if(RotateMinuteHand() != null)
{
StopCoroutine(RotateMinuteHand());
}
//StartCoroutine(RotateMinuteHand());
if(RotateHourHand() != null)
{
StopCoroutine(RotateHourHand());
}
//StartCoroutine(RotateHourHand());
minuteTimer += 60f;
currentMinute += 1;
if(currentMinute == 60)
{
currentMinute = 0;
}
}
if(currentTime > hourTimer)
{
hourTimer += 360f;
currentHour += 1;
if(currentHour == 12)
{
currentHour = 0;
}
}
yield return null;
}
}
private IEnumerator RotateSecondHand()
{
currentSecondRotation = secondHand.localRotation;
newSecondRotation = Quaternion.Euler(0, -secondHandAngleOffset, 0);
secondHandAngleOffset += 6f;
secondHandStartTime = Time.deltaTime;
secondHandEndTime = secondHandStartTime + rotationTime;
while(Time.deltaTime < secondHandEndTime)
{
secondHandRotationTransitionPercentage = (Time.deltaTime - secondHandStartTime) / rotationTime;
secondHand.localRotation = Quaternion.Slerp(currentSecondRotation, newSecondRotation, secondHandRotationTransitionPercentage);
yield return null;
}
}
private IEnumerator RotateMinuteHand()
{
currentSecondRotation = secondHand.localRotation;
newSecondRotation = Quaternion.Euler(0, -minuteHandAngleOffset, 0);
minuteHandAngleOffset += 6f;
minuteHandStartTime = Time.deltaTime;
minuteHandEndTime = minuteHandStartTime + rotationTime;
while(Time.deltaTime < minuteHandEndTime)
{
minuteHandRotationTransitionPercentage = (Time.deltaTime - minuteHandStartTime) / rotationTime;
secondHand.localRotation = Quaternion.Slerp(currentSecondRotation, newSecondRotation, minuteHandRotationTransitionPercentage);
yield return null;
}
}
private IEnumerator RotateHourHand()
{
currentSecondRotation = secondHand.localRotation;
newSecondRotation = Quaternion.Euler(0, -hourHandAngleOffset, 0);
hourHandAngleOffset += 6f;
hourHandStartTime = Time.deltaTime;
hourHandEndTime = hourHandStartTime + rotationTime;
while(Time.deltaTime < hourHandEndTime)
{
hourHandRotationTransitionPercentage = (Time.deltaTime - hourHandStartTime) / rotationTime;
secondHand.localRotation = Quaternion.Slerp(currentSecondRotation, newSecondRotation, hourHandRotationTransitionPercentage);
yield return null;
}
}
#endregion
}
Your answer
Follow this Question
Related Questions
few question about quaternion slerp 3 Answers
Transform rotation and position on key input: rotation only working the first time 2 Answers
Flip over an object (smooth transition) 3 Answers
Rotating clockwise or counter clockwise using Quaternions and PID 0 Answers
help me to rotate an object 1 Answer