WINNING LEVELS - How to make a Video Game in Unity (E09)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we create something that's been missing for a long time that is of course a way to complete a level we'll have a look at triggers UI loading the next level in the queue and the most fun part animation let's go let's begin by creating a trigger that will place at the end of our level let's go to the hierarchy right click go through the object and then cube it's also switch to 3d mode here reset the transform on the cube hit F to focus on it let's set the X scale to 15 the same as our platform let's let the Y to something like 5 and the Z to something like 5 is walk will also drag it up a bit let's set the Y position to 3 this way it stands on our platform let's switch to top mode by clicking the y-axis and switch to isometric by clicking the centre we can now zoom out and drag him to the end of our level let's also rename this to something like end and point and trigger anything you like and you will notice right now that if we switch back to perspective view we can of course see the trigger and we don't want this to be noticeable in-game let's just go ahead and disable the mesh renderer you can of course remove it completely I just like having it here for easy positioning well of course keep the box Collider but remember to check is trigger and if we go ahead and put a script on this it should actually trigger it but in the editor can be kind of annoying if you can't see your object let's just select our object go up here and select the box and we can actually choose an icon I'm going to select the green label here and now even though our object is invisible we always have this green label to show where I endpoint is now we're going to close click it to select it pretty cool let's now create a script that will communicate with our game manager to let it know that we've reached the end of our level to do that we go add component and we call it something like and trigger let's say create an ad and double click it let's to lead our using tags and our two methods not just like we can detect the collision by using void on collision enter we can detect if something has hit our trigger by using void on trigger enter note that on collision entry will not work if your collider is marked as trigger you need to write ontriggerenter instead then open access in parenthesis and some curly brackets now whenever we hit our trigger we need to tell our game manager to display some UI in the screen to do that we first need a reference to a game manager of course we could use find object of type as shown in the last video but I think here it makes sense to just reference it directly let's create a new public game manager and call it game manager with the non-capital we can then go down here and ride game-manager dot and now we have the ability to call a function on the game manager of course we haven't really created a function that we want to call you now let's go back into unity and open up our game manager script here we have code for ending the game if we hit something and then restarting it but we also need a function to call if we actually succeed let's go in here and create another public Lord again remember the order doesn't matter it could be up here down here and even below the restart method as long as it's not within another method so let's write public void and it's call this one complete level if now I'm just going to go ahead and throw a debug that large statement saying something like level 1 let's save that go back into in trigger script and now we should be able to access that function game manager dot and then complete level of course we also need to actually make this reference let's find our end object and drag our game manager under the empty slot now if we hit play and actually managed to reach the end of our level you can see that it displays in the console level 1 this will also display if any other object collides with our trigger so make sure it doesn't touch anything but our player if you have problems with other objects touching the trigger you can use pretty much the same technique as we did with our plan collision to check what object has collided and only act if it was tagged as player so currently displaying a message in the console it's a bit boring let's instead show some UI to let the user know that he did it that's going to the canvas right-click and create a new UI panel let's also switch to 2d mode and hit F to focus on it as you can see a panel is basically just an image that currently fits the entire screen and it will always resize to fit it instead of using the default background force image I'm actually going to go in here and change it to none this just means that our image will be a blank color I'm going to make it completely white and remove the transparency by bumping up the Alpha I'm going to rename it to something like level complete and I'm going to right click on the object go UI and add some text I'm going to scale up the boundaries of our text centered on both the X and Y and change the fonts or something Roboto let's choose Roboto thin let's bump up the font size to something like 100 and let's change the text to level I'm also going to change the name of the object to level and then duplicate it and this one is going to be named complete let's also change the text to complete I'm doing in capital to increase the dramatic effect let's change the font here to Roboto bold drag it down and I'm holding down shift only drag it on one axis let's bump down the font size to something like 53 and I'm just going to put them a bit closer together like both of the objects and drag them up a bit as well very simplistic but I think it looks pretty good and this will of course also scale without screen also I want to take this text object the display soft score and just rename it to score it's good to stay organized finally let's take our level complete and disable it this way we don't start with level complete on the screen let's now take our game manager and add a bit of code first off we need a reference to our UI so that we can enable it when we complete the level to do that we create a public game object and we call it something like complete level UI then under our complete level function we write complete level UI dot and then set active and in here we can write false if you want to disable it or true if you want to enable it and of course we want to enable it so we write true let's say that go into unity reference our UI notice that I can create a reference even though the object is disabled let's try and hit play and we should now see that once we reach our trigger the UI will display it's not currently animated in any way so it looks a bit boring but at least it works let's go ahead and create an animation to do that we find our level complete and re-enable it so we can see what we're doing you also need the animation window so let's go window animation make sure you choose animation and not animate door that's the difference there the animation window as you can see here has a timeline and allows you to animate different properties on any object in unity the animator is responsible for playing animations and transitioning from one animation to another because we only need a single animation here we don't need to focus on the animator but on the animation let's go to the animation window here let's say create let's create a separate folder because animations click this bag up let's call this folder animation and under this let's create a new animation called level complete notice if we go to the project panel that is now a animation folder and inside our queue assets the first one is the animation itself we need to disable looping because we only want the animation to happen once the second one is the animation controller and this is what we edit in the animator notice how I can drag this animation into the animator and how we can make transitions from one into another now this is of course the same animation so it's not going to do much but imagine we can have a whole network of animations in here to create very complex behaviors I'm just going to undo those now you can see that it's automatically added the level complete animation I made it orange this means that it will play at the first animation that means as soon as our level complete gets enabled so we don't have to trigger it anywhere it's just going to play by itself let's go to the scene you so we can see what we're doing and let's go back to the animation window as well notice how the play buttons are red that's because we are in record mode and now any changes we make to the object are going to be recorded as keyframes for example if we go forward about a second go into the color of the image and pump down the alpha we can now see that a keyframe is created at the 1 second mark and at the 0 second mark in the beginning the alpha is normal and it's then going to start fading out and this is only happening on the background of course you need to reverse this so we'll check our two keyframes and change their position we can also make it a bit quicker there we go so now our background fades in and we probably need to do something similar to our two text objects so let's select our level that maybe go a bit forward and change the Alpha to zero let's do the same thing with our complete now again we have to flip these keyframes let's take the bottom two keyframes here which is the Alpha of our complete and level text move that forward and move these ones back let's have them start fading in a bit later than the background so now if we play this back you can see that it fades to white and then the text fades then I think that looks pretty cool so now that we're satisfied with that we can go out of record mode and we can once again disable our level complete object and that's it if we now play the game this animation should automatically play awesome now we can do a lot more with the Unity animation system the last thing that I'm going to show you here is that if we zoom out and go forward a bit we can actually have things happen through script at a certain point in our animation in our case we want to load the next level after a few seconds let's go forward towards the two second mark I'm just going to put it here and add an event now this animation event allows us to select a function you can see how many things are here by default what we want to do is create a small script that sits on the double complete object in order for the event to work it has to sit on the same object that we are animating and the script will just load a new level so let's go here and add a new component this one will call something like level complete we can double click it to open it up in Visual Studio we can clean up as usual and now we can create a public void called something like load next level notice how even though we don't have any more code than this we can now save this and when we go back into unity and select our animation event draw a new function and go down a bit at the very bottom we see load next level so now whenever we reach this point in our animation it's going to automatically call load next level in our script and now in here we can change the scene for us to do that we need to write using Unity engine dot scene management then down here we write scene manager dot load scene and then the name of the scene that we want to load but of course the scene that we want to load is going to depend on what level we are currently on we just want to load the next one in the queue so what we do is instead of using the name we use the build index remember if we go into unity go file and then build settings you can see all of the different scenes that we want to include when we export the game currently we only have one here and that's level 1 each scene in this list has a build index that's the number to the right so what we can do is simply ask unity to load the scene with the build index that is equal to our currently loaded scene plus 1 so in case we are on level 1 which has an index of 0 we want to load 0 plus 1 which is 1 which is going to equal level 2 so to do that in Visual Studio we simply write scene manager dot get active scene to get the scene that we're currently on and then dot build index to get the build index of the scene where county on and then we write plus 1 and we close it up and that's actually all we need to do so we can now save this and of course in unity we need to now make sure that there's always another level waiting if we just run out of themes it is going to create an error so just make sure to create a scene congratulating the player when you speed all the levels I think we'll have a look at doing that in the next video for now we can just go ahead and duplicate our current level so let's save our current scene by hitting ctrl s command s if you're on the Mac it's then select level 1 press control D to duplicate let's double click on level 2 and we can now see that level 2 is open up here and what we can do to make this look a bit different is just delete all of the obstacles except the first one and we can maybe just move this over so if we save this level to looks like this another one looks like this if we then drag level 2 under level one we can see that to build indexes and if we now run level 1 we should see that when reach the end of our level it's going to display the UI and then load the next scene and of course if we reach the end of that scene it's going to throw an error because there are no more scenes added to the build settings but we'll fix that don't worry that was pretty much all I had to show for this video we're nearing the end of the series I still want to dedicate at least one more video to wrap things up and then we can have a look at exporting everything to the platform that you want the series has been really fun to make so far but I don't want to have too many videos in one series instead I want to make more videos to spend that on subjects that can apply to many games that of course also means that it will make lots of more videos that are relevant to this series so don't worry lots more to come of course make sure to subscribe if you aren't already all right thanks guys I'll see you in the next video thanks to of the awesome people who donated in February and a special thanks to Derek huge Kirk faithful Merrifield James Calhoun and Jason Latino if you want to become a picture in yourself you can do so at pitching become slash brekkie thanks a lot guys
Info
Channel: Brackeys
Views: 926,368
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#, win, winning, level, levels, complete, completing, load, scene, scenemanager, manager, transition, unlock
Id: Iv7A8TzreY4
Channel Id: undefined
Length: 11min 57sec (717 seconds)
Published: Sun Mar 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.