How to Manage In-Game Time [Unity Tutorial]

Sdílet
Vložit
  • čas přidán 29. 08. 2024
  • Lots of games use in-game time, Stardew Valley, Animal Crossing, Grand Theft Auto! In this video I'll show you how you can add it to your own games as well as how to easily trigger events depending on the time of day using events/delegates/actions!
    Join me and learn your way through the Unity Game Engine, the C# language and the Visual Studio editor. Remember, if this video was useful then DROP A LIKE! 👍
    Mucho Pixels: muchopixels.it...
    💯 Want to help me out and allow me to keep making these tutorials? Consider supporting the channel on Patreon:
    / comp3interactive
    😍 AWESOME high quality 3D game assets available here:
    shop.runemarks...
    🤩 SPONSORED LINKS:
    www.gigatank30...
    / gigatank3000
    💬 Join the Discord community here:
    / discord
    📱 Find us on social media for more Tips and Tricks:
    / comp3interactive
    / comp3interactive
    / comp3int
    📱 Play our games for FREE here on Google Play:
    bit.ly/2TisAQo #UnityTutorial #Unity3D #Comp3interactive #GameDev

Komentáře • 61

  • @mammels
    @mammels Před 10 měsíci

    Hey thank you so much, I couldn't figure out how to make a if statement with strings but this video help me a huge ton and save me some much time. Thank you

  • @simulation3120
    @simulation3120 Před 2 lety

    I wish I could have seen your inspector on TimeText, but overall much appreciated and helpful.

  • @finalrenderanimationstudio

    Each time I think of systems not usually explored, shortly after I see you covering something Abt it, truly this was a great video,learnt alot too! Lol, got me thinking what's next? 😄

  • @marto561
    @marto561 Před 2 měsíci

    Just brilliant!

  • @neekitsart990
    @neekitsart990 Před 3 lety

    Thank you so much I've been struggling to find any useful tutorials about this

  • @xil2331
    @xil2331 Před 3 lety +2

    This is so cool and helpful! Loving this content!

  • @damdakos
    @damdakos Před 3 lety +3

    Great tutorial. I learned a lot. Thanks.

  • @deepandrence4605
    @deepandrence4605 Před 2 lety

    Thanks a lot. You helped me a lot more than many videos and blogs. Thanks again.

  • @danutorr
    @danutorr Před rokem

    Fantastic vids, this and the previous one as well!

  • @boycececil3727
    @boycececil3727 Před 2 lety +1

    Great JOB!!! THX a lot, keep going !

  • @SkylorBeck
    @SkylorBeck Před 2 lety +2

    Interesting way to do it. I personally prefer using an unsigned int or double, then increment it by one every fixed update. Then each fixed update just check to see if the current tick count is remainder 0 for your fixed update interval and increment another uint (or double) that represents seconds. Now you have a timer for ticks and a timer for seconds. You can remainder at different speeds to change your in game clock speed as well.

  • @pietro0games
    @pietro0games Před 3 lety

    I thought about doing something like this, thanks for proving it

  • @ogulcanzorlu
    @ogulcanzorlu Před 3 lety

    Great practical example on Actions.

  • @bluebaldwin
    @bluebaldwin Před 2 lety

    Really great tutorial and explanation! Keep it up pal 🤙 cheers

  • @Andrew-mg3ib
    @Andrew-mg3ib Před 2 lety +3

    I think using DateTime and TimeSpan solves the redundancy of making your own time structures. And then you just change the update loop to add a minute every time an in-game minute passes.
    And then call a method that checks if hours, days, months, years have changed which fires events.
    If you wanted to handle days and months, you'd have to rewrite the complex code to do so, not to mention doing time calculations if you want to progress time.

    • @Kriskopower
      @Kriskopower Před 2 lety

      Please can you help me with that...if you have the time to explain me the details in creating such a system of course. I would appreciate it very much.

    • @Andrew-mg3ib
      @Andrew-mg3ib Před 2 lety

      ​@@Kriskopower Google those c# structs. There's lots of documentation on them. Way more robust with lots of problems already solved. Was surprised he didnt just use them.

    • @Kriskopower
      @Kriskopower Před 2 lety +2

      yup, yesterday i researched the DateTime built in struct and i made it work easily in my game. Just using the AddMinutes method and updating each frame accordingly.Also i really liked that the constructor is really rich, giving you the opportunity to define w/e time you need. Pretty neat :)

  • @KevlarOxyTheARTofVideoGames

    Awesome... need to get my head around Actions, but still, great video... thanks and subbed :)

    • @comp3interactive
      @comp3interactive  Před 3 lety

      I do actually have another video on my channel soully about Actions that you might find interesting!

  • @christinaVennegerts
    @christinaVennegerts Před 3 lety

    Amazing Tutorial, thank you very much!

  •  Před 3 lety

    Impressive tutorial. Thank you for help.

  • @ThePancakeJedi
    @ThePancakeJedi Před 2 lety +3

    Wouldn't it kill performance by invoking an event every (half) second?

  • @lukegamrivella2101
    @lukegamrivella2101 Před 18 dny

    Thanks for the tutorial! Quick question, how will I keep record of my time across different scenes ? Won't the time reset to 00:00 when switching scenes ?

  • @user-tf7oj8er9q
    @user-tf7oj8er9q Před 6 měsíci

    Thanks for your great job! tell me please what can be wrong if i see 11:60 not 11:00?

  • @archanachaurasia7811
    @archanachaurasia7811 Před 2 lety +1

    Sir can you make the day night cycle using this time manager. It would be a great help

  • @astronuggie
    @astronuggie Před 3 lety

    When I drag the TimeUI script to my clock object which is text, it says The script doesn't inherit a native class that can manage a script. I'm trying to only have in game time, not have it trigger actions yet. I can drag TimeManager but not UI

  • @Wilhelm9218
    @Wilhelm9218 Před 2 lety

    I tried this and for some reason it the clock just keeps going up constantly per frame, regardless of what I set as the float (like in the example 0.5).

  • @lordlemonhead2732
    @lordlemonhead2732 Před 2 lety +2

    If I let my player walk into another scene, does the time continue where it left off or will it restart at its coded starting point?

    • @Definitely_a_Fox
      @Definitely_a_Fox Před 2 lety

      A little late to the party but it would most likely restart.
      A simple way to make sure it continues is to make sure the object that keeps track of the time doesn't get deloaded when a new scene is loaded in.
      Simply add this line;
      DontDestroyOnLoad(this.gameObject);
      and it'll persist!
      I believe that the time will continue to tick on during loads, but I haven't tested it myself to make sure.
      Just make sure that you only ever have one of these time management objects in your scene at once, or it will cause some funky behaviour.

  • @Kriskopower
    @Kriskopower Před 2 lety +2

    Thanks. I do however have a problem.
    I tried to use this to implement day and night cycle in my project.
    Simply if the hour is 20 (20:00pm) i begin to decrease the RGB values of my SpriteRenderer image so it becomes darker.
    Then i check if the hour is 4 (04:00am) for example and i begin to slowly increase the same RGB values back to 255 to kinda simulate sunrise.
    It works fine and looks nice but after the first cycle it just doesnt happen anymore. Clock keep ticking like normal but my checks never get checked again
    and i dont know why :( I have added the same line as you added for the minute : "If the minute is >= 60 we reset it to 0 and we increment Hour by 1: hour++"
    but i added the same line for hour so if the hour >= 24 i say Hour = 0. So yeah the text clock appears to count normally but my checks only happens once for the
    first cycle and never again and i dont know why. Can you please help me with that ?!

    • @Wilhelm9218
      @Wilhelm9218 Před 2 lety

      Were you ever able to get it working? I’m having some difficulties as well. Are you calling your methods that decrease the values in an update function? My guess is that they are just being called once instead of in update where they would be checked every frame?

  • @riskscratch
    @riskscratch Před rokem

    question: i did the code but the update function literally does not work. i tried everything and it just doesn't run. the start function works just fine. any idea what could be causing this?

  • @MrEastgalaxy
    @MrEastgalaxy Před 2 lety

    damn some great tips.

  • @elite_ramsay1667
    @elite_ramsay1667 Před 2 lety

    I was wondering how would i make it so on a button to pause the timer and one to resume the timer

  • @breecebossy8435
    @breecebossy8435 Před rokem

    Thanks. I love the simplicity and versatility. I found that if I subscribe to hour and minute changed, then set something to happen at 6 && 0 (respectively), the method will execute on start, regardless of the hour condition not being met, as the 0 minute condition is instantly met. I fixed this by unsubscribing for minutes. Have I misused the system, or am i using it as it should be used?

    • @charg1nmalaz0r51
      @charg1nmalaz0r51 Před rokem

      You were using it wrong. What is happening when the minute event riggers is anything subscibed to that minute event will run its function, same with the second event. If you hook up one function to two different events it will fire whenener those events fire. What you could do is sign it up to a timechanged event which passes the listener a struct that contains all the time information then you can check on your object when the time changes is the hour and minute 60 if so do this.

  • @epiphanyatnight8732
    @epiphanyatnight8732 Před 3 lety

    Awesome tutorial! Would you recommend using UnityEvent instead of Actions? I usually consider UntiyEvents to be simpler but would love to know why you chose actions.

    • @comp3interactive
      @comp3interactive  Před 3 lety +1

      The benefits of using one over the other are mostly negligible, UnityEvents are slightly slower and come with more of an overhead, but they're visible in the inspector
      Personally I just use Actions coming from a non-gamedev C# background

    • @epiphanyatnight8732
      @epiphanyatnight8732 Před 3 lety +1

      @@comp3interactive Thanks for the reply, really helpful! Keep up the great work, I subbed!

    • @comp3interactive
      @comp3interactive  Před 3 lety

      Thanks man! Hopefully there'll be some more stuff on the channel you find useful too!

  • @abdou023
    @abdou023 Před 2 lety

    Is it okay to still subscribe and fire Actions if the script is on multiple GameObjects like an Enemy script would be on several enemies?

  • @mrmacka1456
    @mrmacka1456 Před rokem

    What object is the TimeBlock?

  • @F0r3v3rT0m0rr0w
    @F0r3v3rT0m0rr0w Před 2 lety

    Quick question, the clock has a jittery effect, What would be the best approach to stopping that ?

    • @comp3interactive
      @comp3interactive  Před 2 lety

      You can use the tag to force monospace, that will stop the characters jumping around

  • @jesseyules
    @jesseyules Před 2 lety

    Q: What code would we need to trigger an animation using bool? (ie .SetBool("drive car", true);

  • @MrReallefauve
    @MrReallefauve Před 3 lety

    Great

  • @mrmacka1456
    @mrmacka1456 Před rokem

    It says "Can't add script component 'TimeUI' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name can be found". Does anyone know how to fix this problem?

    • @do_it_like_rei
      @do_it_like_rei Před rokem

      Same problem here with the time manager script

    • @do_it_like_rei
      @do_it_like_rei Před rokem +1

      I FINALLY GOT IT WORKING FOR US! Okay so first make sure you have "using System;" in the assemblies at the top of the page, under using UnityEngine, etc. For the UI, make sure you also put "using TMPro;" up there or else you can't hook up the text to the script in your clock. Also in your public class make sure it's the name of your script. The time manager script wasn't working for me because for some reason it was set as New Behavior.

    • @ia2604
      @ia2604 Před 24 dny

      @@do_it_like_rei I was just getting hung up on this when your comment set me free. Many thank yous!

  • @random_precision_software

    Can you tell me how much it cost for a sponsor please? Just asking for future reference for when I release my next game.

    • @comp3interactive
      @comp3interactive  Před 3 lety +1

      Hi man. Thanks for inquiring. Currently I can't accept new sponsors for my videos as I'm already sponsored by GigaTank3000 and don't really want to hammer ads and sponsors on the videos and turn people away, but the rates I charge are on my patreon, the sponsor tier is £50 for a months worth (4) videos

    • @random_precision_software
      @random_precision_software Před 3 lety

      @@comp3interactive ok m8

  • @akaRDJC
    @akaRDJC Před 3 lety

    idk if anyone can help me here but i wanna display am and pm strings that appear when the counter reaches 12 hours, am not looking for spoon fed answer as i wanna figure it out myself but if anyone has a hint in the direction id need to go to solve it that would be nice

    • @leebirchenough8890
      @leebirchenough8890 Před 3 lety +1

      did you sort it?

    • @xkittencatx
      @xkittencatx Před 3 lety +4

      Hi there! I only briefly glimpsed at the video, I'm in the process of rewatching it rn, but off the top of my head I would think that you'd make an if-else function checking if the hour counter is equal to 12 which would be P.M and then another function checking if the hour counter is equal to 24 A.M or vice versa. Basically, you'd be operating off of military time.
      But, I feel as if more people prefer the 12-Hour time set up we see on wall clocks and what not. In that case, I'd write some code that changes the 24 numerals back into the 12-Hour but changing the string to read P.M and A.M when necessary.
      I don't know if this helps or not but let me know!

    • @charg1nmalaz0r51
      @charg1nmalaz0r51 Před rokem

      just do something like : string ampm = (if hours > 12

  • @domsim123
    @domsim123 Před rokem

    Гарик Харламов?