How to Manage In-Game Time [Unity Tutorial]
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
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
I wish I could have seen your inspector on TimeText, but overall much appreciated and helpful.
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? 😄
Just brilliant!
Thank you so much I've been struggling to find any useful tutorials about this
This is so cool and helpful! Loving this content!
Great tutorial. I learned a lot. Thanks.
Thanks a lot. You helped me a lot more than many videos and blogs. Thanks again.
Fantastic vids, this and the previous one as well!
Great JOB!!! THX a lot, keep going !
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.
I thought about doing something like this, thanks for proving it
Great practical example on Actions.
Really great tutorial and explanation! Keep it up pal 🤙 cheers
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.
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.
@@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.
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 :)
Awesome... need to get my head around Actions, but still, great video... thanks and subbed :)
I do actually have another video on my channel soully about Actions that you might find interesting!
Amazing Tutorial, thank you very much!
Impressive tutorial. Thank you for help.
Wouldn't it kill performance by invoking an event every (half) second?
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 ?
Thanks for your great job! tell me please what can be wrong if i see 11:60 not 11:00?
Sir can you make the day night cycle using this time manager. It would be a great help
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
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).
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?
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.
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 ?!
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?
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?
damn some great tips.
I was wondering how would i make it so on a button to pause the timer and one to resume the timer
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?
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.
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.
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
@@comp3interactive Thanks for the reply, really helpful! Keep up the great work, I subbed!
Thanks man! Hopefully there'll be some more stuff on the channel you find useful too!
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?
What object is the TimeBlock?
Quick question, the clock has a jittery effect, What would be the best approach to stopping that ?
You can use the tag to force monospace, that will stop the characters jumping around
Q: What code would we need to trigger an animation using bool? (ie .SetBool("drive car", true);
Animator.SetBool("driving", true)
Great
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?
Same problem here with the time manager script
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.
@@do_it_like_rei I was just getting hung up on this when your comment set me free. Many thank yous!
Can you tell me how much it cost for a sponsor please? Just asking for future reference for when I release my next game.
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
@@comp3interactive ok m8
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
did you sort it?
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!
just do something like : string ampm = (if hours > 12
Гарик Харламов?