Hello
My Issue:
My in game clock can tell time but when I want it to move time forward won’t do it. At 7:00 pm I want my character to go to sleep and wake up at 10:00 am but what is happening is that it won’t. I’ve used ints, and true/false statements and both don’t seen to work. When using an int to wait until 10:00 am it will never stop even if it says 10:00 am. With true/false statements it will work for 2-5 seconds but nothing will change.
public class GameClock : MonoBehaviour {
public const int hoursInDay = 24, minutesInHour = 60;
public float dayDuration = 600f;//24 hours of ingame time is 10 minutes real world time...
float totalTime = 0;
float currentTime = 0;
public int WakeUpTime;//thi is for when you wake up from sleeping...
//day night cycle light source...
public DayNightManager DayNight;
public Sleep mySleep;
public bool WakeUP;
public bool MorningTime;
public bool AfternoonTime;
//12 hour clock UI
public TMP_Text digitalClock;
//analog clock
public GameObject clockFace;
public Transform hourHand;
public Transform minuteHand;
const float hoursToDegeers = 360 / 12, minutesToDegrees = 360 / 60;
void Start()
{
WakeUP = false;
}
void Update()
{
totalTime += Time.deltaTime;
currentTime = totalTime % dayDuration;
//Digital Clock
digitalClock.text = Clock12Hour();
//Analog Clock
hourHand.rotation = Quaternion.Euler(0, 0, -GetHour() * hoursToDegeers);
minuteHand.rotation = Quaternion.Euler(0, 0, -GetMinutes() * minutesToDegrees);
DayToNight();
}
public float GetHour()
{
return currentTime * hoursInDay / dayDuration;
}
public float GetMinutes()
{
return (currentTime * hoursInDay * minutesInHour / dayDuration) % minutesInHour;
}
public string Clock12Hour()
{
int hour = Mathf.FloorToInt(GetHour());
//hour = 11;//the starts at 11 o'clock...
string abbreviation = "PM";
MorningTime = false;
AfternoonTime = true;
if (hour >= 12)
{
abbreviation = "AM";
hour -= 12;
MorningTime = true;
AfternoonTime = false;
}
if (hour == 0) hour = 12;
return hour.ToString("00") + " : " + Mathf.FloorToInt(GetMinutes()).ToString("00") + " " + abbreviation;
}
void DayToNight()
{
int time_Hour = Mathf.FloorToInt(GetHour());
int time_Minute = Mathf.FloorToInt(GetMinutes());
if (time_Hour >= 6 && time_Minute >= 30 && AfternoonTime)//if time is greater or equales 1 o clock then change color of day to another
{
DayNight.NextColor = 1;
}
if (time_Hour >= 6 && time_Minute >= 45 && AfternoonTime)
{
DayNight.NextColor = 2;
}
if (time_Hour >= 7 && AfternoonTime)
{
DayNight.NextColor = 3;
mySleep.SleepCollider.GetComponent<BoxCollider2D>().enabled = true;
}
if (time_Hour >= 7 && time_Minute >= 15 && AfternoonTime)
{
DayNight.NextColor = 4;
}
if (time_Hour >= 7 && time_Minute >= 30 && AfternoonTime)
{
DayNight.NextColor = 5;
}
if (time_Hour >= 7 && time_Minute >= 45 && AfternoonTime)
{
DayNight.NextColor = 6;
}
if (time_Hour >= 8 && AfternoonTime)
{
DayNight.NextColor = 7;
}
if (time_Hour >= 10 && MorningTime == true && AfternoonTime == false)
{//this is to make the color normal again...
DayNight.NextColor = 0;
WakeUP = true;
}
}
}
The second script is used to control when player can wake up from sleeping and advances time forward.
public class Sleep : MonoBehaviour
{
public Player myPlayer;
public DayNightManager DayNight;
public GameClock myClock;
public GameObject SleepCollider;
public TMP_Text CanNotSleep;
public GameObject ImageFade;
public bool CanGoToSleep;
public bool CanClickYButton_Sleep;
public bool NoSleepBool;
public bool IsAwake;
int hour;
void Start()
{
hour = Mathf.FloorToInt(myClock.GetHour());
CanNotSleep.enabled = false;
CanGoToSleep = false;
}
void Update()
{
GoToSleep();
}
void GoToSleep()
{
if (CanGoToSleep == true && NoSleepBool == false)
{
StartCoroutine(SleepTime());
}
}
IEnumerator SleepTime()
{
SleepCollider.GetComponent<BoxCollider2D>().enabled = false;
CanNotSleep.enabled = false;
CanClickYButton_Sleep = false;
CanGoToSleep = false;
NoSleepBool = true;
myPlayer.speed = 0f;
float endValue1 = 255;
float endValue2 = 0;
float duration = 5;
float time = 0;
float startValue = ImageFade.GetComponent<Image>().color.a;
bool starFadeBool;
starFadeBool = true;
if (starFadeBool)
{
while (time < duration)
{
ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, Mathf.Lerp(startValue, endValue1, time / duration));
time += Time.deltaTime;
}
ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, endValue1);
}
myClock.dayDuration = 21f;
yield return new WaitUntil(() => hour >= 10 && myClock.WakeUP == true && myClock.MorningTime == true && myClock.AfternoonTime == false);
Debug.Log("WAKE UP...");
myClock.dayDuration = 600f;
yield return new WaitForSeconds(1f);
starFadeBool = false;
if (!starFadeBool)
{
while (time < duration)
{
ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, Mathf.Lerp(endValue1, endValue2, time / duration));
time += Time.deltaTime;
}
ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, endValue2);
}
myPlayer.speed = 0.002f;
myClock.WakeUP = false;
IsAwake = true;
yield return new WaitForSeconds(2f);
IsAwake = false;
yield return null;
}
}
I thank anyone who help out.
Thank You.