How To Make a Countdown Timer in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to today's tutorial video we're gonna be showing you how to make a simple countdown timer in unity now I already showed you how to do this in my four hour long tutorial video where I made this like top-down twin-stick shooter version of the game Doom which is playing right behind me as you'll see and anyways I had kind of like a countdown timer at the beginning where it just says three two one go and a countdown timer like this is something that you could use in pretty much any game so I wanted to break out this little section of the tutorial and show you this kind of simple setup that I made and I will be providing the project files for the things used in this video so make sure you hit the link in the description to download those also if you found this video helpful and it taught you something feel free to hit that like button and please subscribe to the channel for lots more videos on game development also if you have any questions for me or suggestions for future videos feel free to leave those down in the comments section below and if you think the game playing behind me is really cool and you want to learn how it's made again you can check out that for our tutorial that I made where it goes in-depth into everything to create you know the whole whole game front to back so starting with the menu going into game play user interface and then ending with a game over screen it's a really cool tutorial and if you're interested definitely go check that out but with all that out of the way let's go ahead and jump on into the tutorial so we're gonna start here in unity and I'm just gonna show you how I have everything set up as you see I kind of have this HUD here so we have some like a timer and some game information on the top here and then in the middle of the screen we actually have the countdown timer which is gonna say 3 2 1 go so if you don't know how to create a unity UI you can just go up to this create here and go to UI and this is just a text object for the countdown timer so I'll show you how I have it set up here it's under the overlay canvas and I have it sitting in an empty game object called the HUD container and then here we have the countdown text so we're gonna start on the overlay canvas I'm just gonna point out a couple things have this set up so for UI scale mode I have it set to scale with screen size and the reason I'm doing this is because people are gonna be playing our game likely on a bunch of different resolutions and so this just makes it set up so everything kind of scales properly for different resolutions and then the reference resolution that I have is 1920 by 1080 just like a nice sixteen by nine widescreen because that's probably gonna be the most common and then four match width or height I just have a set right into the middle here moving down to the HUD container basically this is just a rect transform that I have set to stretch in both the vertical and horizontal directions and I have everything set to zero here so it stretches to the full width and height of the overlay canvas so on the countdown text object itself I have the X&Y position set to zero for the placeholder text or just have it say three of course you can have this say whatever we want because it's going to override that once we get into our game the font size that have the max for a standard unity text object which is 300 for the alignment I have this set to Center and middle alignment so it's gonna show up in the exact center of our screen and then I set the horizontal and vertical overflow both to overflow I'm just using a pure white color here and then I did add an outline and then with all the outlines I like to take the Alpha Channel off just bring that all the way up to 255 just so we get some nice hard lines around our text here so now let's implement some of the logic of the countdown timer so of course we're going to need to use a c-sharp script so for the tutorial video that I made the four-hour one I put everything for this in the game controller script but as you can see that's kind of a lot going on here so for this tutorial video I created a countdown controller script and that's what I've opened up now and so there's gonna be a pretty simple script however we do have to include one more namespace we're gonna do using unity engine dot UI and so that's gonna allow us to modify the UI components here and then so getting into the actual script here we're going to need to declare a few public variables so the first is going to be a public integer and this is going to be the actual countdown time so normally when we use time in unity we want to use floats and that's so we can get real precise precision for fractions of a second and things like that however because we're just doing simple countdown timer that's three to one we're going to use integers and basically what we're going to be doing is D criminalistic sterols and then we'll call this the countdown display and so this is going to be a reference to this countdown text object that we have here in unity so the way we're gonna make our countdown timer actually ticked down is by using something called a KO routine and a KO routine if you're not already aware is very similar to just a standard function in c-sharp however we can use what are called yield statements to essentially break out of the ko routine and then return back to the KO routine at a later time so basically what we can do is we can have our Co routine execute some code just as a normal function would and then we can say come back to this in five seconds after five seconds then we can continue on with the rest of the code in the Cove routine so the way we actually declare a covert eeen is by saying I in numerator and then here's where we put the actual name of the KO routine so we'll call this countdown to start and then just very similar to a function we do open and close parentheses and then open and close the curly brackets here so within our covert chain we're gonna start off with a while loop so a while loop is gonna execute all the code within it while this statement is true and the statement we're going to test is when countdown time is greater than zero so if our countdown time were to start at say three it's going to execute everything within the while loop while it's at three and then we're going to decrement that so it's going to be at two it's gonna still execute everything in the while loop one is still gonna execute everything once it's at zero it's gonna skip over that while loop because this condition right here is going to be no longer true so while the countdown time is greater than zero so we still have time to countdown we're going to set our countdown display dot text so this is the actual text component of our countdown text equal two countdown time so our actual number got to string and then so this basically just converts the integer number into a string because we can only assign strings to this text component here so now we're going to do a yield return statement and then the yield return statement this is kind of the point of the code where it says hey break away from this co-routine for now and then come back to it after a certain certain parameter is met and in this case we're going to do a new waitforseconds and we're gonna pass in the number of seconds we want to wait of course we just want to wait one second so we'll pass in one F to make sure that we're passing in a float variable instead of a double so after one second and we were returned to our code all we're going to do is take the countdown time and we're going to do - - so it's going to decrement it by one and that's what we need to do for a while loop so again what's going to happen is if our countdown time is set to three so three of course is greater than zero so we're going to display three on the screen we're going to come back to our code in one second we're going to decrement it to two and two is still greater than zero and then so we're going to display to return in one second Deak remit down to one one of course is still greater than zero display one two the screen wait one more second Deak remit down to zero we're gonna loop up we're gonna check 0 is not greater than zero so we're going to skip over this while loop here so once a while loop is complete basically that means we can actually begin the game so one thing that I do want to do to let our player know that the game has begun we're set the countdown display dot text equal to go and so this is just to make it extremely clear to the player that the game has begun and they need to go and start killing some demons right away and so as game designers you always need to keep things like this in mind to make sure you're giving enough information to the player so they know what exactly is happening in your game but it's a little bit more than just printing out go to the screen we actually have to tell our game that is in the game playing state so I have this function on my game control so do game controller dot instance dot begin game and so this is going to trigger some things to allow the player to start inputting controls also going to start the timer in the level so the player can see how long they've been playing this particular level for and just some minor things like that so as of right now our countdown timer is complete its gonna say three two one go and actually begin the game however if we were to leave it as is it's just gonna say go on the screen forever and we don't necessarily want that so we're actually going to do another yield return statement so we'll do a yield return new again we'll do a wait four seconds and then we'll just do another one second delay and this time what we're going to do is to countdown display dot game object that set active passing in false and so this is just going to disable the countdown text so it's no longer showing up on the screen so now we have everything in place for our countdown timer to work however we need to start the co-routine so we're going to actually do that in the regular start function here inside that we're going to call this method called start ko routine and then here we just pass in the name of the ko routine which is countdown to starch of course passing in the open and closed parenthesis semicolon at the end so as soon as the game starts so we're gonna start this co-routine and it's going to do the countdown to start so it's just gonna say three two one go and then we're actually going to begin the game and then after one second we're going to clear off the go text on the screen so go ahead and save that script and come back over into unity and now we're going to need a game object you put the countdown controller on I'm just putting it on my game controller object now we have to set up a couple things of course so we can set our countdown time to any number we want we could do like a thousand if we wanted but for now I'll just show it as five just to show you how it's kind of working here and then the other thing that we need is the countdown display so of course that's on our overlay canvas and that's this countdown text here so we'll just drag that on so now we're ready to test our game so if we just go up and click play here you'll see that we start counting down five four three two one go as soon it says go you'll notice that the timer starts counting up from zero and the game has begun we can actually start to move our player around of course like I said you can put the countdown time to whatever you want so if we did like a thousand we could hit play here and then our countdown time will literally start counting down from a thousand and there's nothing I can do but wait a thousand seconds to play my game but we don't want to do that to our players we just want like a nice three two one so we'll put the countdown time as three go ahead and hit play one last time as a three two one and go and so that is how we can make a simple countdown timer in unity and that's gonna wrap up this tutorial video I really hope that you enjoyed it and you learn something if you did make sure you hit that like button also feel free to subscribe to the channel for lots of more video game development content of course if you have any questions for me or suggest you for future videos feel free to leave those down in the comment section below and don't forget if you want to learn how to make this top-down twin-stick shooter game based off of the game doom feel free to check out that tutorial that I made that again goes in-depth showing you everything front to back of how to create that game really go ahead and check that out I think you're gonna enjoy it but anyways I hope you have a fantastic rest of your day I'll see you in the next one [Music]
Info
Channel: Turbo Makes Games
Views: 60,973
Rating: undefined out of 5
Keywords: unity tutorials, unity3d tutorials, beginner unity tutorials 2019, video game programmer, develop games faster, c# (programming language), video game industry 2019, unity tutorial for beginners 2019, unity ui tutorial 2019, unity 2d game tutorial, c# tutorial, unity3d college, unity tutorial 2019, c# tutorial for beginners, countdown timer, unity countdown, count down timer, video game countdown timer, video game clock, FaZe cLoCk, unity timer, unity time, timer in unity
Id: ulxXGht5D2U
Channel Id: undefined
Length: 12min 33sec (753 seconds)
Published: Fri Oct 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.