GAME OVER - How to make a Video Game in Unity (E08)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to turn our game into an actual game by making a way to lose that's right we'll create a game over stay pretty central to any game this one included let's dive right into it let's begin by dragging our score script under the scripts folder just to clean up from the last video and currently the way we keep track of whether or not we've collided with something it's using a script that sits on the player called player collision and while it makes sense to detect player collision on the player object itself the play object is not a good place to end our game that should be done using some kind of game manager let's go ahead and create one that's right-clicking a hierarchy to create an empty object let's reset the transform and its name it game manager let's also drag it to the top just to make it clear to see let's then add a new component and it's called that game manager as well let's say create an ADD and this component will be responsible for changing States in our game you can do stuff like start and stop a game restart a game and display UI on the screen if you want a score counter a game over screen or transition to other menus like a main menu our level selector in our case we're going to make some code that will restart our game when we hit something or when we fall off the edge let's double click the game manager the first thing we need to do is delete our two standard functions let's also delete the two using tags up here let's then write a new custom function void and let's call it something like end game or game over I'm just gonna write end game here two parentheses and then the curly brackets and in here we'll do some different stuff but for now let's just display a message to the console to let us know whether or not we're calling the function at the right time let's write debug that lock and then insert something like game over we can then save this and hit back into unity so now our game manager has a function called end game that will display a message in the console but we need a way to access our game manager and call this function to do that we go to our player find out player collation script and double click on it normally when we need to access something within another script we use the reference you can see up here we're using a public player movement called movement in order to be able to disable and play a movement when we collide with something and we could go ahead and do the same thing with our game manager here we could write public game manager and then call it something like game manager then save that an indie inspector now drag you know game-manager and this will work fine in our instance but if we wanted our player to die at some point meaning that we would have to remove him from the scene and then maybe spawn him back in because the game manager doesn't sit on the same object we would lose this reference it's actually really easy to show you this let's go and do assets and let's drag our player in here to create a prefab out let's then delete the player from our scene and drag him back in you can see that under game manager it now says none because the prefab itself is not linked to the game manager and that's not really a good way around this so instead we don't use a variable let's delete this here what we do is we actually search for the game manager when we want it and unity has a nice way of searching for scripts to do that let's go to the place in our code where we want to end the game which is right around here and it's right find object of type and the type that we want to find is the game manager we also need to put a open and close parenthesis the syntax is actually very similar to get component which allows us to find a component on the object here we also use the small than and greater than signs and inside we write the component that we want up here for example we could actually replace movement the reference to a player movement script with getcomponent and then make sure to write player movement like this an open could close some parentheses this is actually going to do the exact same thing and it's the same thing with I find object of type we can actually now just use dot and access all of the stuff under our game manager we can then write end game and you will notice that it doesn't show up the reason for this is not that we don't have a valid reference to the game manager but that our game manager actually hides this function that's because it's currently marked as private we need to instead go in here and write public void save that and we should now be able to access it in here so let's write dot end game and you can see that it now shows of course whenever you use fine object of type you need to make sure that you do at least have one object of the type you're searching for or this is going to return an error so just make sure you have game manager in your scene so let's save this head into unity and we should see the variable disappear but that's alright because we're searching for it without a variable if you try and play the game now we're actually going to get some errors you can see here that it says unassigned reference exception the variable player score has not been assigned and the variable of follow player has not been assigned that's because both on our main cap we referenced the player and we need to drag him in there and under a text object that displays our score we also have a reference to the player so we need to drag him in there as well that's two other places where we could use find objective type if we wanted to have the ability to swap a player in and out of the scene I just wanted to show you how it's done do note that it is of course not as performant as creating a direct reference as we do with a variable so let's clear those errors and when we now hit play we should see that when we collide with something it says game over in the console but we don't only want to display this message when we collide with something we also want to display it when we fall off the edge in order to do that let's go into a player again and now let's open the player movement script in here we keep track of the player's position and the forces that we want to add to him in order to make him move inside of a fixed update we can go ahead and add another line and what I want to do is I want to check if our position on the y-axis gets below a certain number that means that if our player falls down and we just say negative one he has fallen off the edge and we want to restart the game so to do that through code we write if then we use our beat up position and we access the y-value we then check if that is less than and I'm just going to hard-code in a small negative number I'm gonna put negative one let's again open or close some curly brackets and if we reach this position we can do the same thing as we did in our player collision we can say find object of type the type we're looking for is the game manager and we can then write dot and game so once you get easy to the syntax it's actually really easy to do we then save the script hit back into unity and we should now see that when we fall off the edge here we see a bunch of game over statements in the console the reason why is that we're checking for this y value each fixed update call that gets done a lot of times a second but we actually only want to do this once we don't want the game to end multiple times so what we do is go inside of the game manager and that's a little nifty trick we can use here all we want to do is create a boolean up here remember boolean czar used to store a value that can either be true or false I'm gonna call this one game has ending will default it to false then in our end game method will set game has ended to true and now we can make an if statement that checks whether or not our game has already ended so we'll say if game has ended its equal to false well then we to do these things then we want to actually end the game so the first time our in-game method is called game ascended is going to be false and so this is going to be true that we are going to set game has ended equal to true we're going to show game over in the console and we could do other stuff like restarting the game and then the next time it's called game ascended is now going to be true and so the if statement here is going to return false and this is not going to be cold so our game will only end once if we save this now we should see it in unity as well quit play and jump off the edge here we can see that only one message is displayed in the console and it doesn't matter whether or not we are colliding with something or falling off the edge it will always only display one so the final thing we need to do is actually restart the game and we do that again inside of our game manager first off I want to create a separate function for this let's call this one void restart we then need to make sure to call this method we do that by writing restart up here so now when our game ends and after we show a message in the console we're going to call the restart method which means that in our code will jump from here to down here and everything inside these two curly brackets will be executed and restarting a scene in unity is actually fairly simple first off we need to go up here and include unity engine dot scene management we need to use this whenever we need to change to a different scene or reload the scene that we are already on we then go into a restart method and rewrite scene manager then press dot and you can see all of the stuff that I've seen manager can do it can merge scenes load scenes get information about active scenes and some other more advanced functions we want to load a scene and the scene that we want to load is currently called level 1 we can see in unity that that's the name of our scene but if you have multiple levels in our game it's not always going to be level 1 the scene that we want to load is the scene that is currently active so we can actually get the active scene by going scene manager get active scene again open a close some parentheses make sure you have all of the parentheses in here and we then use dot name so this piece of code returns the name of our current scene and this piece of code loads the scene with a given name everything is done using the scene manager so we now close this with a semicolon and when we say this and head into unity we should actually see it working however there are going to be a slight problem with this let's hit play we can see that when we collide with the block it instantly restarts but it looks a little weird and also it restarts very very quickly we want a slight delay so we can actually see the Commission the first problem is with the lighting and it's something that unity has been doing recently which is a little bit weird if we go to window then lighting it will open the lighting panel in here we can see that this small check box down here is set to auto this means that unity will automatically detect changes in lighting and do the necessary processing to show an accurate lighting representation however when reloading scenes in the unity editor unity doesn't have time to do these calculations and so we get a scene with very very bland lighting all we need to do is disable auto and if the lighting in your scene now looks weird simply hit build and unity is going to calculate the lining of course this means that every time we change something it's probably a good idea to go back and hit build and I normally leave Auto on when testing but if it's annoying you a lot you can disable it and you can now see that when we hit play and the level restarts our lighting looks perfect however we still need to code in a slight delay to do that we go back into our script and instead of directly calling the function we use something called invoke let's write invoke and invoke takes two parameters the first one in quotation marks is the name of a function which is restart the second one is the delay the amount of time before calling the function and we could definitely just hard-code this in we could write to here and it would wait two seconds but I think it's better to go up here and make it into a variable this way we can easily tweak it in the inspector the variable is going to be a public float that means that we're storing some kind of decimal place number we'll call it restart delay and set it equal to one by default we can then take a restart delay and put it in place of the number two here so now whatever our restart delay is equal to is the amount of time that we will wait before invoke is going to call the restart method and are seeing it's going to load let's save this head back into unity and hopefully everything is going to be working now we just need to add play and when we collide with something you can see that we get a second to see the collision which looks a bit nicer and it's the same thing when we fall off the edge the final thing that you might need to do if unity complains a little bit when reloading the scene is added to the build settings to you that you go to file build settings and in here we have a list of all of the scenes that we want to include when we export our game currently there's nothing in here and that's not a problem because we only have a single scene and unit is smart enough to know that that's the same we want to use but if you have multiple scenes it's very very important that you add all of them in here we can just click Add open scenes or you can always just drag them from the project panel you can see that our scene has a name a checkbox to say whether or not it's included and a build index if you have multiple levels you normally download them by name but by build index so that unity will transition to the next scene in the queue it's pretty cool we can just close that now let's also take our game manager and drag it under the scripts folder and that should be it nothing should change in our case but it should hopefully fix some errors if you have any that was pretty much it for this video I hope you enjoyed it if you did make sure to subscribe so you don't miss a future episode thanks for watching and I will see you in the next video thanks of the awesome people who donated in February and a special thanks to Derek Hughes Kirk face'll Merrifield James Calhoun and Jason Lotito if you want to become a picture in yourself you can do so a pitch intercom slash practice thanks a lot guys
Info
Channel: Brackeys
Views: 1,028,480
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, over, gameover, lose, end, state, ui, on, collision, enter, oncollisionenter, manager, gamemanager
Id: VbZ9_C4-Qbo
Channel Id: undefined
Length: 12min 10sec (730 seconds)
Published: Sun Mar 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.