How to Make an In-Game Timer in Unity - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to today's tutorial video where I'm be showing you how we can make a simple clock for your unity game as you'll see in the game playing behind me in the upper left hand corner I have this timer that's counting upwards that basically times how long the player is in the level for and it's real nice for like speed running and other types of games where there is sort of like a time component that you want to get through a level as quickly as possible now you may have noticed at the beginning of the level there was this countdown timer that said three two one go I'm not gonna be covering that in today's video but I did break that out into a separate tutorial video which is also on my channel of course that tutorial video as well as the one that you're watching right now are both parts of this four hour long tutorial that I made that goes through everything of how to create a twin-stick shooter from start to finish literally everything that you need to know from a menu to gameplay to kind of tracking some things within your game all the way up to a game over screen and it's just a really nice good overview of how to make an entire game especially if you're starting out with unity however I do think these timers are useful in many more games than just twin-stick shooters so I decided to break this out into its own video and if you do enjoy this video and you found it helpful make sure you hit that like button also feel free to subscribe to the channel for lots more videos on video game development of course you've got any questions for me or suggestions for future videos feel free to leave those down in the comment section below and if you'd like to download the assets featured in today's tutorial video feel free to use the link down in the description below but anyways let's go ahead and jump on into the tutorial as you can see I'm over here in unity and first I'm just gonna show you kind of how everything set up here so I have this overlay canvas and the canvas mode is set to scale with screen size the reference resolution is 1920 by 1080 and that's just so I can have a nice 16 by 9 aspect ratio driving down further I have a HUD container which is just set to stretch the entire height and width of the overlay canvas and then you'll see I have this time counter text here and the time counter text is anchored to the upper left-hand corner of the screen and then I kind of use these values to off the edge just a little bit here basically here I just have some placeholder text and this is how I set up the whole font and then I did put a just one pixel outline on it to make it stand out just a little bit more so that's essentially how the UI is set up again you can download that in the link in the description below alright so here I've created this timer controller script I'm going to kind of walk through it and show you everything that it does before we do anything within the actual timer controller class we do need to include a couple namespaces so we need to make sure that we're using the system namespace as well as the unity engine dot UI namespaces these other three should be included by default now moving down to the actual timer controller class itself we are going to need to make a public static timer controller called instance basically this is sort of an easy way to implement a singleton and it's going to allow us from other scripts to call functions on the timer controller nice and easily so in order to set that up again all we do is just declare this public static timer controller instance and then within the awake function we set instance equal to this so just pretty simple and that will allow us to call functions again from outside the class so right now if we're say on our game controller and our begin game function is called within our game controller we can just do timer controller dot instance that begin timer and it's going to go ahead and call this begin timer function which we'll be getting to in just a sec a couple variables that we do need to set up we just have this one public variable which is a text variable for the time counter and that is a reference to this actual time counter object so again it's this time counter text that I have set up here in unity next we're going to go ahead and declare our private variables so the first one is going to be a private timespan variable called time playing so this timespan is part of the system namespace and it's what's actually going to allow us to format the time in a really nice manner so we don't need to kind of do it manually or anything here we just have a simple boolean true/false variable for timer going and lastly we're gonna have a private float variable which is going to be our time so once again in the awake function all we're doing is setting the instance equal to this next in the start function which is called after the awake function we're going to set the time counter text equal to time colon zero zero colon zero zero period zero zero and so this is basically just kind of how we want our time to be formatted of course basically what we're going to have here is we're going to have minutes : seconds period fractions of a second and so this just initializes everything to zero zero zero so when we start our game it's just gonna start at all zeros anyways we're also gonna set the timer going variable to false now the next thing that we're gonna do is have a public void function for begin timer again this is what we can call from something else such as our game controller script by doing timer controller instance begin timer so this will call this function here and it's gonna set this timer going value to true it's gonna set the elapsed time to zero because we're just beginning the timer here and then it's gonna call a start co-routine update timer and the update timer co-routine is kind of where all the magic is going to be happening here so we just have this private eye enumerator called update timer inside it we have a while loop which is just going to continue looping through this as long as the timer going value is set to true so the first line that we're gonna do within this while loop is do elapsed time plus equals time Delta time so this is going to take the elapsed time value which starts at zero and then we're going to add time.deltatime to that value and set it back to the elapsed time so time naught Delta time if you're not already aware is basically the time that it took from the previous frame to the current frame it's that time value in between and so the idea is that we're gonna be looping through this while loop every single frame and then so we're going to be adding that time difference on to our elapsed time every single frame sorry elapsed time stays current next we're going to set our time playing timespan so the way we do that is we a time playing equals time span dot from seconds so this is just a function built into the time span class which converts just a straight floating-point seconds into this time span here so we just pass in the elapsed time because this is a float value and then it generates this time span instance so next we're actually going to go ahead and display this to the screen so first we need to create a new string value called the time playing STR we're going to set that equal to time and of course this is gonna be whatever we wanted so we can make it say like time in game or something like that but for now we'll just go ahead and leave it at time then we're gonna add in the string for time playing dot to string and then here is where we kind of specify the formatting of how we want our time to actually be displayed on the screen so I'll leave the link to some documentation kind of down in the description below but basically we have this whole string encapsulated and double quotes you'll see there's double quotes on either end of this here and here's where we can kind of specify what we want to be displayed so if you wanted hours you could actually do like ehh of course if we want minutes we want minutes first we can do mmm seconds RSS and then the F's are for fractions of a second and we can actually go up to I believe 6s here so we can get pretty specific of a time value now you'll notice there's a little bit more than just MS and s's and F's going on here so basically so we have the MM for minute minute and I will do a single quotation mark then a colon then another single quotation mark and so this is going to basically put this colon character in between our minutes and seconds value and then we're going to do the same thing between the seconds and fractions of a second by doing single quotation marks with a period in between so this is actually going to be displayed in unity just like this you'll see that it's time colon 2 colon 3 5 point zero 5 actually because we have this set to mmm it's going to actually be displayed as Oh - Oh three five oh five now we could change some things a little bit so I mean we could put maybe spaces around that if we wanted space between minutes and seconds or if we didn't want a period between it we could maybe do a dollar sign or even like the letter T and so here's how that would look over in unity of course you'll see that we have now the spaces around the colon and we have the T between the seconds and fractions of a second however that's not what we want I just want mine to be displayed like this so now once we have that string all nicely formatted we'll just go ahead and set the time counselor dot text equal to the time playing STR and the last thing we need to do in the while loop is do a yield return null which means it's going to return to this point in the Cove routine on the next frame so it's gonna go ahead and render this frame and then on the next frame it's gonna come back to this it's gonna go ahead and check the while loop and to make sure that the timer going value is still set to true if it is it's gonna run through all this and just continually updating the timer each and every frame now if we do want to end our timer again we have another public function set up it's a public void end timer all it does is assess the timer going value to false and then so what's gonna happen is when this code returns to it and it checks if it's false it's gonna go ahead and exit out the while loop and complete the operation of the KO routine here of course we can just save this and go back over into unity and whatever object you have this timer controller on you don't want to go ahead and just make sure you set this time counter to this time counter text here so everything displays properly so with that all set we're about ready to go ahead and test out our game so as you'll see that our placeholder text is just set to all these funky numbers but that doesn't matter because when we actually start our game it's going to zero out as we explicitly told it to do and you'll see that we now start counting up seconds and fractions of a second and then when it actually counts all the way up to 60 here it's gonna roll over and it's going to count one minute here so we can just go ahead and wait that out so I can prove to you that's actually what's happening 58 59 and 60 in the night rolls over to one minute here so then of course you know seconds start continuing up and yeah so that's pretty much a basic timer so I mean looks cool right so anyways that's pretty much I can create a basic timer in unity if you did find this video helpful and you learn something make sure you hit that like button also feel free to subscribe the channel for lots of more videos on video game development of course you've got any questions for me or suggestions for future videos feel free to leave those down in the comment section below and if you do want to download the project files featured in this video you can go ahead and use the link down in the description below but I hope you all have a fantastic rest of your day and I'll see you in the next one [Music] you [Music]
Info
Channel: Turbo Makes Games
Views: 49,416
Rating: undefined out of 5
Keywords: unity tutorials, unity3d tutorials, beginner unity tutorials 2019, custom editor window, game tools programming, game tools programmer, video game programmer, how to program video games, develop games faster, how to make a timer in unity, unity timer, tutorial, how to make a clock in unity, unity clock, timespan, time span, unity timespan, time in unity, keep track of time in unity, how to
Id: qc7J0iei3BU
Channel Id: undefined
Length: 12min 1sec (721 seconds)
Published: Wed Oct 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.