Search Unity

starts fight at midnight in-game time

Discussion in 'Scripting' started by nadarabus, Apr 22, 2021.

  1. nadarabus

    nadarabus

    Joined:
    Apr 19, 2021
    Posts:
    3
    Hello everyone,

    I am really new here and I could not find any other similar topic. So I am creating a simple game and I implemented in-game time in the UI.
    Now I would like to start a specific game event (no cutscene) at midnight in-game time. How could I do that? Any link to tutorial or video would be great because I could not find any.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You'd track whether the event has occurred already, probably with a simple bool. Then you'd check if time is later than midnight, and if the event has not occurred. If both are true, you fire the event and set the bool tracking whether the event has occurred to true, so further checks whether you should fire the event again resolve to false. The exact code depends a lot on how exactly you're tracking in game time, which you haven't shared.
     
  3. nadarabus

    nadarabus

    Joined:
    Apr 19, 2021
    Posts:
    3
    public class ClockUI : MonoBehaviour {

    private const float REAL_SECONDS_PER_INGAME_DAY = 60f;

    private Transform clockHourHandTransform;
    private Transform clockMinuteHandTransform;
    public Text timeText;
    private float day;

    private void Awake() {
    clockHourHandTransform = transform.Find("hourHand");
    clockMinuteHandTransform = transform.Find("minuteHand");
    timeText = transform.Find("timeText").GetComponent<Text>();
    }

    private void Update() {
    day += Time.deltaTime / REAL_SECONDS_PER_INGAME_DAY;

    float dayNormalized = day % 1f;

    float rotationDegreesPerDay = 360f;
    clockHourHandTransform.eulerAngles = new Vector3(0, 0, -dayNormalized * rotationDegreesPerDay);

    float hoursPerDay = 24f;
    clockMinuteHandTransform.eulerAngles = new Vector3(0, 0, -dayNormalized * rotationDegreesPerDay * hoursPerDay);

    string hoursString = Mathf.Floor(dayNormalized * hoursPerDay).ToString("00");

    float minutesPerHour = 60f;
    string minutesString = Mathf.Floor(((dayNormalized * hoursPerDay) % 1f) * minutesPerHour).ToString("00");

    timeText.text = hoursString + ":" + minutesString;
    }

    }

    Please see my code for in-game time. I am missing two following thing: How to create link from other script for specific in-game time/day? How to create specific gameplay setup such as boss fight which will be lunch as a event?