Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to make in-game time?

Discussion in 'Scripting' started by PanPrezess, Aug 25, 2020.

  1. PanPrezess

    PanPrezess

    Joined:
    Aug 17, 2020
    Posts:
    8
    I want to make time in the game.
    2 minutes real life = 1 hour in game.

    Thanks in advance!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    It depends on what you want to happen. On a basic level though, you can set
    Time.timeScale
    . E.g.:
    Code (CSharp):
    1. // 2 minutes in real life = 1 hour in game is 30 times faster than normal
    2. Time.timeScale = 30f
    Now that means every piece of code that relies on Time.deltaTime, Time.time, etc... will progress as if time is moving 30x faster than normal.
     
  3. PanPrezess

    PanPrezess

    Joined:
    Aug 17, 2020
    Posts:
    8
    I want to add clock to the game that will display in-game time.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,753
    If you're looking to accelerate it the way GTA does, it's just a number... one minute is one hour in GTA, IIRC. Display seconds as minutes, then minutes as hours, and wrap them at 24 if you want.

    You can do what Praetor suggest above but your game may completely fail if you use physics, because every FixedUpdate() the physics system is going to have to run a LOT of updates. Not only that but the Unity animation system may not be very happy with highly-accelerated timeframes and may miss animation events.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,753
    Yeah, just display seconds as minutes and minutes as hours, then wrap the hours back around to 1 when they reach 12, or back around to 0 when they reach 24 (24-hr time format).
     
    PraetorBlue likes this.
  6. PanPrezess

    PanPrezess

    Joined:
    Aug 17, 2020
    Posts:
    8
    Thanks. I'll do as you say for now but does anyone know the answer to my question?