How to Make a Game - Unity Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there today you're gonna make a game by the end of this video you'll know how to build a game just like what you see on screen even if you've never looked at a line of code or thought about building your own game before you'll end this video knowing how to build a game of your own we'll start with how to get set up what tools you need and how to install them then we'll dive into art and see just how easy it is to pull any art you want into a game next we'll move on to coding in a way that's easy enough for a ten-year-old to understand and by the end of it all you'll be able to customize like crazy and just have fun with your own project before we get going I just wanted to say that if you have questions drop them in the comments below I'll be watching them and try to keep up with them and answer whatever problems you might come up with also if you don't mind please hit that share button if this is the kind of content you're interested in where we do in-depth tutorials on game development it really helps when you share it or at the very least just hit the like button and also if this is a little bit too beginner for you you're looking for more advanced stuff I'd say hit the like button and skip to the end where I talk about how we're gonna turn this into something a lot more advanced that probably fits your needs more alright let's get going we're gonna start with installation and setup we're going to begin with installing unity so if you already have unity installed go ahead and skip past this part but if not let me explain briefly what unity is why we install it and how to get it installed so when you're building a game what you'll usually do is start with an existing game engine unity and unreal are the two most popular game engines and most of the time as a developer when you're starting up a game you'll pick the engine that you like the most or you're most familiar with and you can build the same kind of games in unreal but we're gonna do it with unity today so to get started what you need to do is go to the unity download page that's just that unity 3d comm slash download and it'll redirect you to something like this you have two options here there's a choose your unity plus download which is to install a very specific version of unity so unity releases about 4 new big versions every year and if you want to install a very specific version like I want the version from 2018 quarter one I can go there and just choose it and install that but what I personally prefer and most people that I know use is the unity hub this is a tool for managing your projects and managing your different unity installations it doesn't matter necessarily which one you go with but I'm gonna go with the hub and show you that process and you can just follow along so we'll install the hub and just click on it to download it and it's gonna go right into my downloads folder that I've emptied out so it looks nice and clean when it's done I should see it in my bar in Chrome or whatever browser you're using or I should be able to just go to the download folder and see it right there now we'll install it by double clicking on the Installer which is going to pop up a little dialog a user account control dialog in Windows just click YES on that and then choose agree to agree to the Terms of Service and we'll go for the default installation folder hit install and let it finish when it's done we'll choose run unity hub and then when the firewall pop-up appears just allow access to it once the hub launches you should see an app that looks something like this there's a projects a learn and an install section we need to go to installs first and then choose add to install a version of unity right now I have a bunch of versions installed but you'll probably have none so hit the Add button and then choose the latest official release you can see the version that it currently is on but it doesn't matter if it's a newer version just select that and then choose next and you'll see here that we have a bunch of options for different types of build support so if we wanted to build for Android and make a mobile game we just choose that mobile box and click it to install that setup we could also do the same for WebGL so if you maybe want to share this game on the web when you're done choose that box otherwise you can just ignore most of these options and hit the done button then it will do an install you can see that if we've run out of space though it's gonna give me an error and say hey we don't have enough space available if that's the case then just go clear up some extra hard drive space on your system how you do that I think it's kind of up to you I personally like to use the windirstat tool to find extra stuff or just delete things out of the temp folder assuming that you are not out of space though your install should finish and you'll get a version like this showing up right here the next thing we need to do is create our project to do that we'll go to the project section and then we're gonna choose the new option since I have multiple versions of unity installed I'm gonna use the drop-down and choose the version that I want to use if you only have one installed though you can just click on the button next we'll choose a template I'm going to use the 2d template because we're gonna build a 2-d game but it's worth noting that if you accidentally choose the wrong one it's pretty easy to switch back and forth once you're in the editor it's really just setting up some defaults for the way the editor looks and the way that our camera is set up there pretty simple things to swap back and forth between or even mix and match next we need to look at the location I store my game projects in this git folder just because I use the git source control system I recommend that you create a projects folder or something else off of your root folder on your main hard drive whatever your fastest hard drive is really and keep all of your projects in there when we create a new project it'll be a subfolder of this don't mix it deep into your Documents folder or your downloads folder where it's gonna get lost deleted or just confused with something else keep a nice setup or area for all of your projects now we need to give it a name and I'm gonna name this mad birds and then hit the create button to kick off our project if starting up the first time may take a minute or two so just be patient and wait for those little loading bars to go by and eventually you'll be presented with a UI that looks just like this well here we are we're in the game engine so what do you do well first let's make sure that your layout looks like mine so it's not confusing to do that go to this window option and then look for layouts and just choose default will use the default layout throughout this entire thing to make sure that it's consistent and easy to follow along with now go over this scene view you should see here that we have a scene a game in an asset store tab we're gonna ignore the asset store tab for now we're gonna go back to this scene tab and then put our mouse over this scene area where we can see a little camera icon and a grid if you use your mouse wheel you should be able to zoom in and out just like this and notice that it's gonna zoom in and out based on where the mouse is so if I move it over there and zoom in it's gonna go there if I move it over here it's gonna zoom in on this spot and if I just zoom out I'm gonna get into this weird odd position now if I want to drag this around so that I can see the camera back in the center I can hold down the right mouse button over the scene view and move the mouse that way I can just drag it and move my view all around then I can zoom back in what I want to do is get it so this box right here that's showing me what our camera can see is all in my scene view and visible now when I say that this is what my camera can see what I mean is that we actually have a camera component in a level right now that's able to see something in our game view so if we click on game you see that we have a blank blue screen what's happening is that we actually have a camera set up but we don't really have anything for it to look at let's see how we can modify that I'm gonna go back to the scene view and I'm just gonna left click on this camera and notice that in our scene hierarchy it expanded and our main camera got selected if I click off of it down here you see it's unselected and if I click it again you see it's now selected again so we have our main camera selected which is this camera that's showing up here in this box that's showing what our camera will show our player and we also get a little camera preview again it's still just blue because we haven't put anything in front of it we also have this inspector tab that showed up and gives us a couple options we have a checkbox to allow us to turn our camera off and on again so we're going to toggle it on and off by clicking on it we can rename it just by typing in here call this main camera - or something I'm gonna undo that though we can also adjust the position rotation and scale of this so if I grab the rotation you can see that I can do some weird stuff it doesn't make a lot of sense though when I'd grab this X and rotate it so I'll put that back to zero let's try moving in the Y value so I grab the Y here and I go left and right and you see that the camera goes up and down and if I rotate on the y axis it's a little bit weird too but if I rotate on the z axis it might start to make a little bit of sense with this rotation is doing right we're tipping it left or I guess right or left by dragging the something down and again to drag these just click on them and then hold the mouse and drag left and right or up and down now I'm going to reset these all back to zero just by clicking in the box and hitting a zero we're gonna leave the Z at negative ten though that's the default value I'm gonna put the Y back to zero though make sure that everything else is zero and our scale is still at one there's really not a lot of point in scaling a camera by the way we'll play with scale a bit later though okay so we have our scene here and our main camera and our level right now is called sample scene first let's just try adjusting the background color of our camera to do that we can click on the background again we need to make sure that our main camera is selected click on this background color and we get a nice little color picker where we can go choose a color for our background and if you look here we can see in our camera preview that it's updating and if I go to my game View tab here and then click on this background you can see that we're actually able to update it and see what the game view looks like while it's updating I'm gonna go with something that's kind of a dark green for now we'll change it later though so go ahead and just choose any old color that you like make sure that you understand how we change that color now we're going to create our first sprite to do that we'll right-click in the hierarchy area and then choose to diab jecht because we want to create a new 2d object and then choose sprite we should see a new sprite appear in our hierarchy and our inspector should update to show us info about our new sprite the first thing that I want to do is change this actual sprite value under the sprite renderer you see that it says sprite is set to none and it's got these brackets around the word sprite meaning that it wants us to assign a sprite here now if you don't see this and it looks more like this it's just because it's collapsed you can click this little arrow to collapse and expand each individual section of our hierarchy or our inspector now to choose a sprite I can just click on this little circle option here and it'll show me all of the sprites that exist in my project so far right now we don't have anything really interesting so let's just go with the knob sprite so I've chosen the knobs bright and you notice that we can't really see anything in our scene or in our screen at all now what's happening here is that when we created a sprite using this right-click menu the position of it wasn't really well known it just kind of assumed that we wanted it in the center for our screen and a back somewhere it didn't know what position to set it at so to fix that we can right click on our transform here and choose the reset option all that's really doing is setting the x y&z to zero the x y&z of the rotation is zero and the x y&z of the scale to ones just resetting these back to the default values now if I go to my game view you can see I have a tiny little knob right there in the middle now let's say I want to make that bigger I can go to my scene view and I have a couple options here if we look up here there are multiple tools available we have a hand tool we have a move tool which allows us to move the object around we have a rotate tool and then we have a scale tool we'll get into the other tools afterwards but first go select the scale tool and then it will zoom in with the mouse window a little bit notice that we can't really see it but there is a box here underneath this camera so if I grab this box and just drag it up you can see that our little knob is growing and growing and growing now I'm gonna move to the move tool by clicking on it up here and just drag it over to the right a little bit now if I switch back to that scale tool I think the dot shows up a little bit better and I can scale this thing back down and we still can't really see the dot very well but it's at least somewhat more visible because we don't have the camera on top of it now let's go to the game view and see what that looks like as we've scaled it also notice that these scale values are changing every time we've dragged the scale up and down all that's really happening when we use that scale tool is these numbers right here are getting increased so if I just grab on this red though notice that I'll stretch it out to the right and only the X scale is growing if I grab on just the top one here this green one only our Y scale changes now it's also worth noting that it's scaling our z value but our Z value doesn't matter at all because we're flat the Z value is the depth of this object since it's a completely flat object its scale doesn't matter because no matter how deep we make it x 0 for the actual depth it never gets any deeper imagine just like a 2d flat piece of paper it doesn't matter what we do on the scale on the Z because it won't scale out there but we can scale it on these x and y values we can also go to the game view and just grab these values and scale them up and down it does start to get a bit blurry though this sprite isn't really made for scaling up and down so let's set it back to 1 1 and 1 and just leave our little sprite right there and then let's talk about how we can replace this with something that's not a boring little dot when it comes to finding art for your game there are dozens of places available with free or cheap art that you can download and just pull right into your projects when it comes to building 2d games you can really use almost any 2d image the one thing you'll need to think about is transparency but we'll talk about that more later to get started what we're gonna do is use this open game art org site they have a lot of great options for 2d game building and it's all free for us to download and grab and there's just a lot of selection so you can pick and customize what you want to build here now to find what I want I'm gonna go to this search box and just choose bird and hit search it just Auto filled because I've searched for the word bird on here a bunch of times you can see already that there are many pages of different birds that look like they would fit into this game I'm gonna go with this green guy cuz he's the one that I tested with so download him I'm gonna click on him and it's gonna take me to another page and this page will usually have a list of files available sometimes it'll be a zip file that contains multiple textures or multiple sprites for our character sometimes it'll just be a PNG file in fact let's go just click on another one and see if this one looks maybe a little bit different so here you see that this one has a PNG another PNG and a blend file this is a 3d one but there are a lot of different things that you might see when you're looking at your 2d ones let's go click on just one more and see what these guys look like thinking that they're probably gonna be a zip yep so these ones that are a zip file tend to be animated sprites that have you know the wings flapping or something like that so let's go back to this green one and I'm gonna download this flappy bird sprite flappy fat bird sprite sheets that's it that's the name of it so click on it and download this into my downloads folder now that that's finished downloading I should be able to open the file up and go into this PNG subfolder here and see all of the different frames I can even double click on them to open them up in a viewer whatever your photo editor viewer is by default in your operating system should just pop up and show them to you so to get these files into my project there are a couple options that I have normally if they were just in a folder and not in a zip file I could take them and I could just drag them into the project so I could get rid of my browser and I could just take them right here select all of them I just clicked on the first one held shift clicked on the last one and I'd usually be able to just drag them over but if you notice here I'm getting this little crossed out you can't do that icon the reason for that is that they're in a zip file so the ways around this are pretty simple I can either extract the zip file so I can go to downloads right click on it and then use extract all or 7-zip is the tool that I prefer but I can just hit the extract all option hit extract and then I'll get a folder that has them that it's in a zip I can go into that PNG subfolder and I can select them all and drop them right into my project just like that notice they've all just appeared in my project now if you don't want to do it that way you can also right-click on this folder hit show in Explorer go into the assets subfolder notice that this is my get slash mad birds folder like I said I put all my projects and that get folder in mad birds is the project name so that's the sub folder if I go into this assets folder this is actually a direct mapping of what we see down here in this project view of our unity editor so here you can see I have all of these PNG files and these dot meta files don't worry too much about those files we'll talk about them a little bit later they essentially just store some metadata about how we want to import these images so what I could do again is go in to here and then copy from the zip and paste into here I already have them in though so I'm just gonna close that out and work with the files that I have in my assets folder now so let's say I want to take this bird and I want to put him into my game what do I need to do well I could start by just taking him right here clicking and dropping him into my hierarchy and look at that I now have a big giant bird appearing in my scene let's go to the scene view and say I wanted to scale him down first I'll click on the scene view and I'll zoom out with my mouse wheel and then I'm gonna hit W to switch to that move to a watch hit W and it switch to the move tool I could alternatively just click on it but I'm gonna hit it just so I can drag this guy over a little bit and then go back to that scale tool which by the way is the hotkey e or no naughty are ours this scale tool ia's rotate NW is moved so go to our that's the scale tool and i can now see that box really easily the one that was hard to see on that blank white sprite and grab it and scale him up or down make him bigger or smaller and if I scale him down like this go to our game view you'll see that we now have a scaled-down version of the bird he doesn't look a little pixelated but we'll address that in a moment so right now I've got a bird and a sprite that does nothing and I want to change this up because I have all of these cool little frames for my bird but I don't have any way to make him animate yet I haven't set him up to animate so to do that I can actually make a very small change instead I'll what I'll do is select this frame 1 which is our bird that we dropped out there in our hierarchy and I'm just gonna hit delete to kill him out of the scene in fact I'm gonna do the same for this new sprite go select it and hit delete so now all I have is my main camera now I'll select frame 1 here in our project view by the way if you don't see the project for you just go to project here it's on a tab with the console so you may have tabbed over so on the project view I'll select frame 1 hold shift and select frame 8 so all of them are selected now if I drag this into my hierarchy I'm gonna get a pop-up and it's so kind of an interesting and weird pop-up but it's saying create new animation and it wants me to give this an animation name so I'm gonna call this green bird and it's gonna automatically add the dot annum extension here what it's actually doing is creating an animation for us among or across all of these sprites so it's creating an animation that will play all of these sprites for us kind of automatically notice what we want to do and it's making it so we don't have to do the hard work so I've given it the name I hit save and now I've got my frame 1 I've got this sprite renderer over here that shows frame 1 but it also added this animator component here and we got this little green bird animation file that appeared so let's hit play we haven't hit that yet let's hit the play button and see what our bird does and look at that he's playing his animation and flapping his wings so before we move on and start adding more to this let's talk about how this is working how is he animating how did that magic all just appear because you're gonna want to know how to modify this and make other things animate in different ways so remember how I said that when we dropped all of these out it created an animation and we gave it that name that's this little green bird animation file right here and if I double click on it I'm gonna get an animation editor window so if it appeared off screen for me yours may appear somewhere else and probably isn't attached so let's just take this animation window drag it onto your screen and then I'm gonna dock it right down here next to this project in console window so now it's a third tab all of these windows in here can be docked and attached to each other like this and moved around and rearranged so I've got my animation window on here I'm gonna stop playing for a moment and I'm gonna select this frame 1 this is my bird and if I look through here in this animation window I have a couple options I can step ahead next frame like this look and watch what happens to the bird again I have to have him selected for this animation to play on him so I selected I can just hit play through and see each frame or I can hit the play button and just watch it go I can also go over this part of the animation editor and use the mouse wheel to zoom in and out notice how it has one two three four five six seven eight keyframes and if we go back to our project view we have eight frames here what it actually did is it created an animation and it assigned each frame of this to be one of those animation frames in fact look at the sprite renderer as I go through this see the frame number changing all this animation is doing is saying hey sprite renderer every time we go to the next animation frame change to the next sprite frame so we can have animations that do other things as scale objects move objects rotate objects or even move them around relative to each other in the world but a very simple and starting animation type is to just do this sprite replacement so we swap out the sprite every frame and we get this nice cool flapping bird effect now let's talk real quickly about this animator because the way that this animation it was hooked up to this is through an animator and it's a little bit confusing because we have an animation window and an animator window and I think it's easy to mix them up the way to think about it though is that the animation is a single animation it's like a single type of thing that's happening for our bird we're only gonna have one animation it's gonna be very simple but if we had another thing that maybe switch to animations think of a character that runs and then he switches to a shooting animation or a jumping animation or maybe uh playing a taking hit animation if we have something like that we'd be modifying multiple animations in our animation editor and then we use our animate or to hook them up so let's go look at the animator now I'm gonna go back to the project view and I'm gonna look at this animator and just click on the controller notice that it selects this frame 1 controller this is actually a dot there dot controller file you can see the extension down here and it got the name from the name of this bird frame so I'm gonna rename this I'm gonna click on it just 1 left click or hitting f2 on it should pop up the rename dialogue then we name this green bird it's gonna be my green bird animator in fact I'm gonna go into my scene hierarchy right here and rename frame 1 to be a green bird anyway we can keep it a little bit consistent now I'm gonna go to this animator controller again not not the animation we already looked at that we're gonna go into the controller and just double click on it and we're gonna get that other animator window in fact let's pull up the animation window side-by-side animation has our keyframes animator has this weird state machine thing so the first thing I'm going to do is just grab this green bird section and drag it down so that it's a little bit more in the center of our screen and then we'll talk about what this is so an animator state machine is a way for us to transition between different animations or different animation types again we want to go from flying to crashed or exploded or shooting it's little bird gun we want to be able to set that up in here and we would do that by adding multiple animations and then adding things called transitions between them again since we only have one animation we don't need to do that instead we just have a default animation that plays and whenever we enter our animator state machine our green bird animation is going to play and if I click on it you can see a little bit of info about it it says that the motion here is green bird that's our animation file or our dot a.m. file to go back to the project view and click on it you see it actually selects that so our default animation for this animator it's just that green bird sprite animation and it's just gonna loop over time now the way that we know it's going to loop over time is also I think a little bit confusing if we select the animation though we'll see that there's a loop time option when that's checked it's just gonna automatically loop this animation now I want to show one more thing I want to show what this looks like when we're playing so I'm gonna take this animator window and I'm just gonna dock it over here to the side and just put it maybe right about there so we can kind of watch this window while we play I want you to see what it's doing so we play and we select our bird and when we've got our bird selected we can actually see the animation playing in progress this is showing us the progress of that animation along the way and every time it goes back to the beginning it's just looping again so this is how we set up animation state machines again we're not going to go too deep onto these but I wanted you to at least understand how animation state machines are created and what's all being hooked up behind the scenes when you do this quick little drag and suddenly a bunch of things just work so if you need to change it or something breaks remember you can modify which animation is playing right here you can even adjust the speed and speed it up yeah or even put it in reverse speed the negative one would you think it's kind of hard to tell with a bird making it negative one its wings just flapping I don't know if you can tell the difference but you can adjust that speed and then swap out the animation and also know where to look if something goes wrong and again that's on this green bird in the animator we have the controller and the animation all right let's stop playing now and I'm gonna get rid of this animator window in fact I'm just gonna dock it along here so that it's just another tab and I can kind of ignore it before we get to moving around the bird and resizing him let's pull in a background that we can use for reference and scale so we know how big our bird should be and what he should look like to do that we'll go back to open gamer we can just search for backgrounds and find something that we like I'm gonna go with this nice free background that has a pretty mountain in the background I'll download it by clicking on the free mountain backgrounds it and let it download into my downloads folder I'll open up that zip again and pull it over and go into this transparent PNG folder and then I'll just find the full background image and this time I'm gonna import it in the other way I said before that we could unzip it and just drag it in the alternative is to copy it so I'll right-click and choose copy and then I'm gonna go into my unity project I'm gonna right click in my project Explorer hit show in Explorer which would also be show in finder if you happen to be on a Mac then go into my assets folder and then I'm gonna right click in this folder and paste it in now remember you can do this either way I just wanted to show both ways that you can put files in so that you know you have both options available now I'm gonna minimize this and go back into unity and as long as we're not playing we should see it appear if I was in play mode though this file wouldn't appear until I stopped playing so now I've got this full background and I'm gonna click on it and take a quick peek at it with the background selected we can see all kinds of different import options here now most of the time you're not going to need to change these from the defaults but you do want to make sure that it's set on sprite 2d and UI sometimes the textures that you'll pull in will be set to default and you'll need to change them before working on 2d sprites the default mode is more for 3d objects or things that are rendering on 3d objects the textures that go on top of 3d objects think of like the skin for a 3d character or the side of a building for sprites though we want to make sure that we're on the sprite mode also take a look at the little preview window down here we can grow this make it bigger or smaller you can even right-click on it to pop it out and then you can redock it down here and if you click this little button you can actually dock it back to the inspector it kind of goes off screen though but if I drag it over here see we can hit dock preview to inspector and we can drag that back over also if that disappears it could just be that you left clicked on it which minimizes it just left click on it again to bring it back up so I'm gonna fix my layout because I'd already moved it around a little bit I'd drag this that's why things look a little bit different so remember to change our layout back to the default we can go to window layouts and then default again now I want to take my background and just put it into our level so I'm gonna grab this full background and drop it right into our hierarchy area I could also drop it right here but I'm gonna put it in the hierarchy just so the position goes to zero zero zero now I wanted to pause for just a moment because you may have run into something here it's possible that when you put in your background you don't see your bird and that's because the background could be drawing on top of the bird to control the order things draw and we use the order in layer setting here on the sprite renderer and since our bird and our background are both set at zero it's very possible for the bird to render behind the background or the background to render behind the bird we're not really controlling it and it's gonna be somewhat random so to adjust this you can go to the background and on the order in layer area just put in a lower number than zero so we can go with negative one to force it I also want to show if you're not seeing the issue what it looks like so say I pulled my order and layer up to one or anything greater than zero my birds just missing I can select them I know he's there because I can see the outline and the selection outline but he's not rendering so again just pull the order in layer for your background back to anything below zero I just go with a nice low number so that the background is always back and not blocking things then I'm gonna zoom out over the scene view make sure you're on the scene view not the game view but the scene view use the mouse wheel to zoom out and we can get a good idea of how big our bird is compared to our level but also notice this little white box here that is our camera view so if I go to my game view you'll see that that's what my camera is showing I'll go back to the scene view you'll see hey my camera is only showing this little chunk here so what can I do to fix that there are a couple options that I've got I can move my camera down so I could just select my camera hit W to go to that move tool notice the little arrow popped up but I could also use the Y value here I'm just gonna use the green arrow and I can drag it down and look at that camera preview the camera preview makes it nice and easy for me to see what we're gonna show in our scene so this looks closer to what I want to see for a level the bird is obviously way too big but if I go back to that game view you'll see that hey I can see the mountains I can see the ground I think that's probably good so let's go back to the scene view with our camera down in the right position and if we look at the Y value here you see it's what negative two point three nine we could always round this like negative two point four negative two point five whatever looks good and then we'll go select our green bird and scale him down so remember this white box that little box right there is the outline of what our camera is going to see so I'm gonna take this bird and I'm gonna scale him with our the scale tool or select the scale tool up here grab this little scale dot in the middle and pull down until he looks about the right size now I'm pretty sure I just want to go with a point one here so I'm just gonna go into my transform scale and just type in a point one for all three of these but remember Z doesn't really matter so I can leave that at one or point one now if I go back to my game view I like it that my bird looks a little bit closer to the scale that I would expect to see him at I'm gonna go back to the scene view one more time though go to the move tool which is again W for the hotkey or click on it up here and then grab it with this little blue box and just put him kind of in starting position so I expect him to be right about here remembering that this white line is my camera I'll go back to the game view I can see it right there one thing that's worth noting by the way if you have multiple Mon so you have the screen real estate you can always take your game view and pull it side-by-side and then you can see your game view and your scene view at the same time but there's something weird going on here notice how my camera has changed and my bird is no longer in the camera the reason for that is the game view by default switches to this free aspect mode which means that it's going to render whatever resolution or aspect ratio fits in the window no matter how you scale it so if I scale this thing out notice that it's getting wider in here and scale it this way and it's getting smaller what I actually want to do is change this free aspect option over to something like 1920 by 1080 or 16 by 9 or 16 by 10 whichever one of those you want to choose I generally go with in 1920 by 1080 but you can choose any one of those as your default if you don't see 1920 by 1080 you can always hit this plus and then you can just type in the values that you want so you can put in 1920 and 1080 and it will become a resolution you just hit OK and it's gonna add that right in there I already have it so I'll hit cancel but you can add in whatever resolution you want and it's good to do that for whatever platform you're targeting 1920 by 1080 is 1080p or the standard so that's usually when I just target so we've got 1080p or 1920 by 1080 selected now we got a better idea of what our thing looks like and where our bird sits in the scene so I'm going to move him over just a little bit so I've got a little bit of extra room there now I think he's in position so let's say I want my bird to fall to make an object fall or interact with the world in a physical way we want to use the physics system that's built into unity to do that we simply need to add an extra component on to our green bird and we haven't talked much about components but let's take a quick peek at the inspector notice that we have a transform and then a sprite renderer and then the animator and remember that the animator only came when we made the animated version of the bird if we look at our full background for example it does not have an animator just a transform and a sprite renderer I'm gonna go select the bird again though and look down below at this add component button this allows us to add more components more things like sprite renderers and transforms and animators but we don't want to add two of the same thing instead we want to add something that allows us to use the physics system so I'm gonna hit add component and if I just clear out my search here you see that there are a bunch of different folders and options we want to use the physics 2d section and it's very important that you avoid the physics section when you're working in a 2-d game because it's easy to accidentally misuse and use the wrong component in fact building this thing up I did it multiple times I still consistently accidentally use the 3d physics things when I mean to use the 2d ones so make sure you go to physics 2d and then we're gonna look for a rigidbody 2d option so if I scroll down I should see rigidbody 2d and I can click on it I could also alternatively just start typing and searching and filtering and find it in that list if I go to this add component and just are typing well let's collapse some of these things and show it I start typing I can type RI GI and you'd see rigidbody 2d has appeared but I've already got it added on there so I don't need to add it again so now my bird has a rigidbody 2d on it and before I hit play I'm actually going to save my scene to do that I just hold ctrl s or command S or you can look for it here under file and save whatever your hotkey is there should be just ctrl or command s though so now it's saved and notice there's a little star went away meaning that my level is saved my bird if I go select them has a rigidbody an animator and a sprite renderer and I hit play I should see some magic happen look at that my bird just falls and keeps falling now you might wonder why does he fall and not hit the ground well if you think about it for a moment we don't want our bird to just randomly hit any sprite that's out there in the scene we want things to be backgrounds wewe don't want him like interacting with that mountain we just only want him to fall and maybe bounce off the ground piece there since we have the ground piece as a single object though it's gonna be a little bit weird because our background right now is a giant picture with the ground here it's got the mountains in the background and the sky in the back there so you might think hey well I guess we need to split this up right let's make it into three different pieces maybe it's the mountain in the clouds and then the foreground piece we don't actually need to do that it's much simpler than that so let's get the game view back onto this tab so we can have our scene view big and let's think about other ways to address this what we actually want to do is set up a Collider now a Collider can be set on any sprite or it can be set without a sprite in fact what we're gonna do is create a Collider for our background that doesn't really line up with our sprite so I'm gonna select this full background and I'm gonna add a component to it we'll go to physics 2d and we're gonna look for box Collider 2d and notice that it made a green outline around everything you might not see it but if i zoom in you see this little green outline if I uncheck the box Collider you see that it turns off this is showing us what the things will collide with or what the bounds of our Collider are now since this Collider is way too big in fact it's the full background and I just want it to be up to here we're gonna need to edit it a little bit luckily we can just hit this edit Collider button and then I can take this green dot and drag it down so I can make it so that the collider now only encompasses this ground part and not the full background so now if I save and press play again it should automatically switch to the game view and our bird still falls right through what's going on Jason you lied to me why doesn't this work well the problem is that our bird doesn't have a Collider so when we want to deal with collisions in a game we need to give both of the things that we want to collide a Collider it's essentially saying that this is the hip box or the area where if these things touch counted as a physical reaction again we could have things that we want to pass through walls we might have invisible walls or just background elements or things that you can walk through it I think a lot of games have trees and stuff that you can just walk right through we could be in that scenario so we need to be very specific and assign and define what our colliders are so our bird also needs a Collider so I can go add component and I could go to physics 2d and I can add a box Collider around him but that might be a little bit weird cuz he's not really a box shape so the collider around him in fact let's add it real quick see that the collider around him doesn't really line up with him well and it'll land it'll collide and kind of slide flat along there but he should really roll and bounce and hit off of different pieces of him we can be a little bit more precise with the collider for this so I'm gonna remove this box Collider so again I've got the green bird selected well right-click on the Box Collider that I just added and hit remove we're gonna add component go to physics 2d again and we're gonna look for the polygon Collider polygon Collider 2d and notice that this put a bunch of little green lines around it it's essentially making a Collider around this bird object I'm gonna save my scene one more time and then zoom out with the mouse wheel and then we'll press play and watch the magic happen look at that our bird has fallen he's down so next we need to talk about how we can make him fly how did he make it so he can launch away and go hit enemies and start diving into actually writing some code to create our first code file we can simply right-click in our assets section to choose create and then choose c-sharp script it's gonna pop up create a file and it's going to give us the rename dialog by default if you click off of it you're gonna get this weird named one like that if I click off it's gonna be called new behave and I might not even be able to read the entire name of it what I can do however is use this slider to switch between a graphical view or a ListView then I can see it's named new behavior script I don't want that to be the name though so I'm gonna hit f2 and rename it and I want this to be named bird with a cap be and while it might seem kind of stupid the naming and the case sensitivity is somewhat important so I recommend you go with the capital B and I'm gonna hit enter then I'm gonna hit enter one more time and what should happen for you is your code editor will launch on Windows by default this is gonna be Visual Studio on a Mac I'm not sure what it is now I think it might be Visual Studio code but it should look very similar and the code that you write will be exactly the same so here's what my editor looks like now by default there are a lot of things visible that I don't necessarily need to see you like this output window so I'm gonna auto hide it just by clicking on the little pin and I'm gonna do the same for my project explorer sometimes these can be useful when you have a lot of files in there or there's a lot going on in your project but right now I don't necessarily need them there's also it looks like an update available I'm gonna skip that one for now though and let's just take a look at our file here so there are a couple of things to notice the first is that our file up here says it's named bird bats yes and we can have multiple files here they'll all show up as different tabs but the one that we have selected and open is birds yes but notice here that we have this weird thing that says public class new behavior script which might remind you of the original name of it this is actually going to cause a big problem and I've seen a lot of people struggle with this we actually need to make sure that the word here after this part that says public class matches our file name but not with the dot CS so it should just say bird so to change that I'm gonna double click on it which will just select that whole set of code or that whole word I guess and then put it in bird with a capital B again the casing matters so make sure that you match it then I'll save it with ctrl s you can also find your save hotkey by going to file and then where's that save assets slash bird ctrl s so now we've got a class file or a code file or a script that's made for our bird and it's got a bunch of weird stuff in here so let's talk about this and assume that you've never seen code before let's look at first the left side here we have the line numbers this is the actual line number of code so that we can reference them when we're talking about them or go to them or when we're looking at error messages sometimes we'll get an error message it says error on line 306 and then we can just go here and look for line 306 and figure out what the problem is and maybe try to track it down another thing we can do is in Visual Studio we can hold ctrl and hit G and then just type a line number to go to so say I wanted to go to line 16 I can go to that not super useful when I have a tiny little file that all fits on one screen but sometimes if it's bigger I might need to know what those line numbers mean and how to get around we can also scroll up and down to look through our file and again it's kind of tiny so there's not much there and then we can click on anything and we can even collapse these little sections and expand them out so let's talk about what all of these things mean now at the top you'll see that we have two great outlines and one non great outline they all start with the word using and then they reference something here like this one says using system dot collections the next one says using system collections generic and then the one that's not grey says using unity engine what these are are telling the editor or I guess really the engine that we want to be able to use special methods and classes which are extra code that somebody else has written from these packages or namespaces they're almost like an external package but it's kind of all bundled in it's a little bit confusing if we go into the details you can essentially think of it as a little chunk of code that we want to make available to our code so we're saying in our bird we want to be able to use collections which are things like lists of objects we want to be able to use generic collections which are things like lists of birds we'll talk a little bit more about those later but we're not actually using them right now so we can actually delete them to delete them I can just go select line one hold shift and hit delete and it should just clear out that line I can also go to the end of the line by clicking on it just clicking right at the end I can hold shift and hit the HOME key on my keyboard it'll select the whole line and I can hit delete and it will delete that out again and hit delete one more time to get rid of that empty line so now I just have the using unity in part now let's do a zoom so if I hold down control and use the mouse wheel I can zoom in and out I'm going to zoom it up so that the code is nice and big and we're going to take a look at these other parts of our script here so we have next on line three because we've deleted that unused stuff which by the way it's just there because a lot of the time those things are commonly used in our case we're not going to use them so there's no reason to have them there and cause extra complication and confusion but since they're commonly used set of libraries or methods and classes unity just puts them in there by default so let's look at line three here we have public class bird colon monobehaviour that's a lot it means a lot of stuff here so let's break it down now first the word public here is what we call an access modifier there are really three that you would use there's private public and another one called protected for our case we're going to put the word public before anything that says class now sometimes you want to make a class that is not public and what that means is that it can't be accessed or created by other classes now again we don't need that here and public is what you're usually going to see before any old class so when you see a class you're usually gonna see this public word before it but you'll also notice later on that we'll have methods that are public too so down here we have this void start we may have the word public or private before it in fact if we don't put public here it implicitly means that it thinks we have the word private here and again that just means that this start method wouldn't be able to be called from other classes or other scripts we'll talk about why that's important later though so the next thing that we have after the word public is class now there are a couple different types of files that we could have we could have a class file which is essentially a code script that's going to run some behavior or some actions we could also have a struct which is more of a data storage type where we could have some information that we store on a thing so we create a file we create a struct in there and it just stores some info that we pass around and there are some performance reasons for that and special uses for it but it we're not going to need those here right now we just need a class and we're only going to use classes then we have the name of our class so here we're saying that we want to create a public class and we want it to be named bird or we want to define it we're not really creating one yet we're kind of defining what a bird is the next part is very important and a little bit different in very unity specific so every time we create a class we have the option of using what's called a base class and a base class just means that we don't want to write everything that this object can do from scratch we want to be able to reuse some behavior now in unity if we use this mono behavior base class it actually allows it to be a component that we can attach to our objects in game or in the editor now to set a base class we have to put this Col in here and then we put the name of that base class so whenever we create a new script in unity we're going to automatically see that it gets created with public class the name of the script remember I named it that weird new behavior or left it defaults I had to go in and change it but we'll get the name of the script and it will have a colon and mono behavior because it assumes that whatever code we write we want to be able to attach to a game object or attach as a component in our case that is true there are cases later on where you'll want to write code that's not necessarily a mono behavior but that's when things get a little bit more complex and I think beyond the scope of what we need for building this game so we've got this class here set up I'm gonna save just ctrl s and again double check that the file name matches the class name and that there are no errors if I had accidentally deleted something here and save you see that we have an error we've got this little red squiggly saying hey it expected an ending bracket so I need to put a bracket at the end of update because that's what it's expecting so I'll put that bracket back in the error goes away and I should have no problems now I'm gonna go back into the unity editor so I'm just gonna minimize my code editor I'm not gonna close it just hit the minimize because we're gonna bounce back and forth there's a lot we don't want to reopen it every single time so just hit minimize we'll go select our green bird and then I'm gonna collapse down this rigidbody2d and collapse down the polygon Collider 2d and then I'm gonna hit add component and just start typing toward bird and I should see my bird script show up and then I should be able to hit Enter or just click on it and have it add it as a component now if you don't see it showing up here you need to go back into the code editor and look for errors you probably deleted something added an extra line somewhere maybe not an extra blank line but a line with an extra word on it or maybe you typed the letter k at the end of the file and saved it that will cause an error it'll cause it to not compile so let me clean that up and let's talk briefly about some of this other stuff in here so we have on line 5 this green line with Q slashes and it says start is called before the first frame update now first off it's important to note that these two slashes just mean that it's a comment this is code that's not executed it's just there for humans to read and understand what's going on it's also a comment that's added by default to every script by unity so that people who are new can understand what's going on and where to put a new code or where to put their code when they're starting up a new project so the start method gets called before the first frame update that means that the start method is the first thing that's run on this script so if we add in some functionality here and we want it to kick off right at the beginning we can put it in start what actually happens though is there's an awake method that gets called before that but we don't need to worry about that just yet we also have this update method and it says update is called once per frame what this means is that every single frame our code that we put inside this little set of brackets right after this update is going to run so if we run it at 100 frames a second this code is gonna run a hundred times a second if we run it one frame a second that's gonna run at one frame per second this is where we put things like input handling sometimes movement and other stuff like that maybe timers things that need to update every frame or modify something every frame or just do something every frame start is where we put things that we need to deal with initialization wise like setting up an object so what do we want to do here well first we're gonna take both of these and delete them so I'm going to select from line 5 click drag all the way down to line 15 and hit delete we're gonna empty this class out and write it from scratch we don't need any of that starting nonsense right don't worry it's not gonna be too complicated next we're gonna add in a new method now when we use this monobehaviour base class because we have that there we get access to a whole bunch of interesting and fun to use unity callbacks one of them is called onmousedown so if i just start typing on mouse down i should see the onmousedown option appear in my intellisense which is the autocomplete option i can just click on it just double click it and I should get a method that gets automatically typed out saying private void onmousedown now if that doesn't work you can always type this out you just need to make sure that you have these parentheses at the end which is shift 9 and then shift to 0 for the second one and then you need to have these braces which are the same braces they're the ones next to the P key so you hold shift hit that one next to the P and the one right to the right of that and you'll get these braces here they should automatically complete though and they should automatically add if I delete these and I hit the opening brace with shift in the key next to P I should get a closing brace automatically then I can hit enter and it looks just like that auto-generated section so what do we want to do onmousedown what does this mean well this onmousedown method is going to get called every time we click on our bird it's actually a nice easy way to handle input onto sprites so every time we click onto our bird the first thing I want to do is just change its color let's just make them bright red so that we know that we've clicked on them and that we're holding down the mouse on them to do that though we need to access another component so we have our bird component let's save real quick and then go minimize this and go back to the editor and let's look at our bird one more time so we have our bird component we have a polygon Collider we have our rigidbody we have an animator and we have a sprite renderer now to change the color if you're wondering it's gonna be the sprite renderer in fact if we expand it out you can even see it has a color property I can click on this get a color pop up and just change it to something else and see what its gonna do so if I got a red and see it turns red go to like a green Annie turns green or a purple any turns per so what I want to do is make him turn kind of red whenever we click on him just so that we know that we've selected him and that we're dragging him something that tells us hey the systems at least working before we start getting too complicated so I'm gonna change this back to white just by selecting white up here on the top left and then we'll go back to our bird script and we're gonna say hey sprite renderer when we press the mouse down we want you to turn red now you're probably wondering hey how do we do that how do we access the sprite renderer well luckily accessing components on a game object is a very common thing to do so accessing other components that are next to you can be done with a very simple single call we using this get component so we'll type the words get component and then we're gonna use the less than sign the one shift and the key next to em and then we'll type in the word sprite renderer and make sure you spell it right if you don't spell it right it's not going to turn blue and it's gonna look weird then we'll move over one space to the rice just hit the right arrow on my keyboard and we'll do an open parenthesis and a closed parenthesis what this is gonna do this little chunk of code is going to get my sprite renderer and it's gonna let me do something to it so if I hit dot I'll see all of the options that I have and you might notice that color is already an option available so I can just click on it and maybe hit Enter I think it's a equals color dot red now it's important that you match the casing otherwise things aren't going to work and you're gonna have compiler errors but what we're doing is essentially saying hey get that sprite renderer and then set the color to red whenever we press the mouse down so I'm going to save this minimize and go back into unity we'll press play and give it a try so we hit play we go select our bird hold the button down and look at that he turns red and if I let go of the button hey he stays red because we're not doing anything to change his color back we're just setting it to red and leaving it that way so let's stop playing and let's go back into our bird script say we wanted to make him not red when we let the mouse up well we can do that just by implementing the mouse up method so here I'll just hat out a new line after line a and I'll add one more line because in between these methods I always leave one blank line there's pretty much a common standard to have a one line space between your methods so just add a line there and again it doesn't affect the code functionality it's really about readability and a lot of writing code is about making it readable by humans the computer compiler can read it no matter what it looks like make it human readable first more than anything else so let's add an odd Mouse up so I'm gonna type it out this time say private void on Mouse up again I couldn't let it use the autocomplete and then I'll add the brackets there so again that was shift in the key next to P right after and then we'll say getcomponent I want to just choose this one that has the arrow so I'm just gonna hit the down arrow a couple times and hit enter because I'm lazy I know I hit the less than key to get it into those brackets because it didn't autocomplete to the right one for me and I type in sprite renderer and I'm just gonna go down arrow and hit enter to select it we add those open closed parentheses again and do dot color equals and here we'll just use color dot white if I save that off and minimize we should be able to go back in and see our bird change from red to white when we click and release or well remember his white though is actually a green so we're just adding a red tint on top and if I release its back to green Red Tent release back to green so there we go we now have a bird that reacts to some very basic input [Music] now we need to make our bird fly to do that we're gonna need to implement another method but first I've reopened to my project after closing it and suddenly my bird disappeared so I want to show you what could cause that and how to fix it so let's go take a look if we look in our hierarchy we can see we actually have the bird and we have our background and if I click on the bird I can see it right here getting selected in my scene view but in my game view it's invisible and even in my scene view I only see the box around it if I expand out the sprite renderer section though there's actually an option here called order in layer and this will bring the object behind or in front of other objects that are on the same layer since everything right now is in the default sorting layer to bring this bird to the front and force him to always be in the front I just need to make his order and layer greater than the backgrounds order and layer the default for everything is zero so just changing it to a one will bring him up to the front and this is how you can control what things are behind and in front of other things so just remember if you don't see your object and you can select it and it's a 2d sprite it may be an order in layer issue now let's jump on to making our bird actually fly like I said before we need to make him move first we want to be able to drag him around and then have him snap and fire off to do that we're gonna need to go back into our bird script and add in another method so let's open up the bird script and we need to add something after our mouse down first let's take one more look at our class here so we have this public class and everything inside of these brackets is a field or a method it's either data or something that can act on the bird if we put code outside of this bracket it's not going to be part of our bird class and it's not gonna work so we need to add in a special method another one of these monobehaviour methods and it's called on Mouse drag so if I add an extra line after on Mouse up and add one more because I want that extra space between my previous methods and my new one I'm gonna start typing private void on em oh now I didn't use uppercase oh here or an uppercase M because I'm gonna let it autocomplete so I'm gonna go down and select on Mouse drag and just double click it and it's gonna implement the method again if I put that down here below this line 19 we'd have an error in fact let me just show you what that looks like so I'm gonna take this chunk of code and cut it with control X and I'm gonna go down here below and paste it with control V and look at that we have an error we get the little red squiggly saying that hey there's something wrong here and it says that a namespace cannot directly contain members such as fields or methods what this essentially means is that your methods or fields need to be inside a class they need to be inside these brackets so let's undo that on my keyboard that's just ctrl Z a couple times it just undoes the previous thing that I've done by the way control Y will redo so I can go forward and control Z to undo it's a great way to fix a little mistake if you accidentally delete something break something or you're not sure what you change just hit control Z a couple times you can go back in if it was the right thing you can hit control Y to go forward okay so we've got the on mouse drag method yeah you might be wondering what we do here just think about it for a moment what we really want to do is when we're moving this object around or when we're dragging our mouse around with this object to move underneath our mouse so we want the object to go wherever our mouse is so the first thing we need to do is change the transform position so we can get the transform of our bird without using this get component call we can actually just say transform dot and then if you look here we have position local scale parent local position rotation so position scale and rotation probably sound familiar let's jump back over to unity and just take one more quick peek at that minimize this and look we have position rotation and scale so what we're going to want to do is adjust the position of this object or just the transform component of that so let's jump back over to our code one more time and to do that we'll just say position equals and then we need to give it a position so to read the mouse position we can actually say input dot Mouse position we can add a semicolon to the end of that and save and this is not gonna work but let's see what it does then we'll talk about how to fix it so we're gonna set the position of this thing to our mouse's position jump back into unity and with the bird selected over here so we can see it in the inspector we'll hit play it should switch this over to the game view we can watch the position here and watch as I click on this what happens to his transform position look at that it's at 402 57 if I move it down here it's getting close to about a zero to zero if they get right to the bottom left corner I should be able to get it just about to zero zero I go way up here it's gonna go to whatever my resolution is so 1920 by almost 1080 and you can see as I drag to the right here it's what 1920 by zero on the bottom and on the left you might have guessed this it's gonna be 0 and 19 or Kennedy on the wire close to 0 just look at those position values and for estimate and round them because I'm dragging a little bit outside the edges what's actually happening here is the mouse position is 0 0 at the bottom left so the x value is 0 and the y value is 0 and at the top-right value we actually get whatever our resolution is so we get 1920 because it's 1920 pixels across and 1080 because it's 1080 pixels high or 1080 pixels high so we need to actually translate this we need to change the mouse position which is in these screen coordinates again with 0 0 on the bottom left and the max value is on the top right we need to convert that into world space coordinates just remember if we stop playing and we look at our bird let's just switch to the move tool right now he's at negative 5 comma negative 3 and 0 0 if we set them to 0 and 0 is actually right in the center of our scene it's right in the center of our world so we need to be able to translate these screen space coordinates into world space coordinates we also need to remember that not everybody is going to play it the same aspect ratio or the same resolution so we can't just hard code this stuff we need to use the built-in helper methods that really make it easy I'm gonna undo that move by way with control-z is it works in the editor just like it does in the code and then we'll go back into visual studio and we'll make a little change instead of setting it to the mouse position we're going to use this special method and we're going to save off the desired position in what's called a vector 3 so I'll say vector 3 which is just 8 X Y and a Z position we'll call this new position equals and then we're gonna say camera dot main casing again matters here dot screen point 2 or screen 2 world points sorry screen to world point right here so we're gonna select that one and do an open parenthesis and then we give it a mouse position so what this is doing is we can take the screen space position transforms position from screen space into world space so we're gonna take this screen position of our mouse and we're gonna turn it into a place in the world using our camera so our camera knowing where it's looking and where it's positioned it already knows that can figure out where in the world we're clicking we're moving our mouse when we have this screen point that we give it so I'm gonna cut this input that Mouse position with control X because I don't want to retype it then I'll click right here and hit control V to paste it in again you could always just delete it and type it but I like to short use shortcuts like that okay then we'll go to the end and add a semicolon because we have to end every line of our code with a semicolon so now we're getting a new position and we can say transform position equals and I'll just type in new position so we're creating this new position vector here and then we're setting our transforms position to that position we could also alternatively do this now don't do this right now I'm just going to show you how it could work we could say something like this and delete that and then we're getting this position it's returning it back over to us and then putting it in here we're not gonna do that though because this also isn't gonna quite work so let's undo and get back to this point right here where we have this new position variable we're gonna go in and run it again and see what happens and see why this doesn't work and the last little change that we need to do to make it actually happen so we jump back to unity hit play watch this birds position when I grab him the X the Y and the Z value so I click and his x and y values seemed pretty reasonable if I'm right in the center it's close to zero and we're at 1 ish if I was good at moving things around we I could get to 0 but look at the Z the Z value is negative 10 now the reason for that is that our cameras Z value is also negative 10 we don't want that we don't want to be moving the Z position of this object let's take a look at this with the scene view side-by-side with the game so here we are in the scene view in the game view and if I move it around or I click around I've actually lost my bird if I click around though you're gonna see that the bird is still there but he's way off position so I grab him and look I can see him in my scene view but because he's so far forward my camera actually isn't rendering let's go to 3d mode now so if I click on my scene view there's a 2d 3d toggle button this lets me switch between 2d and 3d mode I'm gonna go to 3d mode hold down the right mouse button and just use a to slide over to the left and then I'm gonna stop playing and start playing again again just hold down the right mouse button over the scene view you can use WASD to move around so I'm gonna do that I'm gonna get a little bit closer to this bird I'm gonna go select the bird I'm also gonna select the camera though don't you see the cameras bounce the camera is rendering everything inside of this box that it sees its facing that direction rendering everything it sees in the box now if I go select this bird over here and grab him look at that he bounced right over to the edge of the camera so he's right up against the camera he was too close and we don't actually render it so we need to make one more change and just not move this birds z position so instead of it being at negative 10 we'll leave it at 0 to do that we'll jump back over to our code and then instead of setting our position to this new position we want to set it to this new position but only the X and y values and we want to leave the Z value alone to do that we can say the position equals a new vector3 and let's just delete that real quick so we do new vector3 and if you look here I hit the down arrow once it actually allows us to just pass in an x and a y value and it will set the Z value to zero so we can do that just by saying new position you know hit down arrow and do dot X and then we pass a new position dot Y so this is going to give us a new vector3 but it's just gonna have a zero for the Z value so if we save that and when we come back in we should see the magic of being able to drag our bird around and get him ready for a launch play and we click and drag and look at that he just moves around and follows my mouse exactly how I want now that the bird our mouse around we need to make a little change so he stops just falling to the ground do that I'll stop playing and we're gonna expand out the rigidbody2d component and just set the gravity scale value to zero this is gonna make him not use gravity and not fall on his own we're eventually gonna turn this back on in code but you can see now I've turned it off and I drag him in he just moves to wherever I drag him so let's talk about launching this bird to get him to launch back let's get the game view bigger to get him to launch we're gonna pull back and then he's gonna fly off in the opposite direction that we've pulled him to know what direction we've pulled him though we need to save off this starting position so we need to know where the bird is when our level first loads or when our bird first gets initialized now I think I mentioned earlier that there's a method that gets called whenever an object is initialized or loaded into a scene and that's the awake method there's also a start method that gets called a little bit later in this case though we want to use the awake method to just save off the starting position of our bird where a bird is placed and then we'll use that in a calculation to tell our rigid body this little component here to make our bird go flying off in the opposite direction or towards that starting point is really what we want so imagine we pull him down to here we want them to fly towards that point if we pull him like this he should fly down towards that starting point - so let's go back to our code and we can add a new awake method right at the beginning of our bird class most of the time the way that I'll organize these methods is I'll have the awake and start right up at the beginning of our class I will put some fields above that and you'll see that shortly after but I usually try to avoid having my awake and start methods in the middle or near the end of the class I want it to be very obvious for anybody that opens up this code that right when it starts up it's doing something and this is what it's doing so let's add an awake now we'll add a line after this line for again inside the brackets very important to be inside our class and just start typing AWA and it'll autocomplete and I'll hit down arrow and enter again you could also type out private void awake open parenthesis close sieze and your brackets but you got to make sure your casing matches so in our awake method we're gonna save off this initial position and we're gonna do that in a variable we don't have a variable defined yet though so we're gonna add one now in fact we're gonna do that above our awake method so I'm gonna go up a above awake again hit enter a couple times one two and then I'll go up here to line five from on the first line after the bracket but I've got another extra line after it just so that it's a little bit easier to read and we're gonna say vector three with a capital v that's this type of an X Y and a Z position we're gonna name this with an underscore so the first character is gonna be shift in the cube just to the right of zero to give us an underscore and this isn't a hard requirement but it's a very common naming practice and I recommend that you follow it and we'll call this initial position with a lowercase I and a capital P so this underscore here is really just to define that this is a private variable that should only be accessed inside this bird class it shouldn't be accessed by anything outside of this bird class and remember I mentioned earlier with the accessibility not modifiers you can put private here or you can leave it blank and it means the same thing if you put public here suddenly this underscore wouldn't make a lot of sense and we would be able to access this thing from outside our bird class we don't have any other classes so it really wouldn't break anything right now but as our projects grow we want to make sure that only things that are meant to be modified by other classes can be modified by those classes so we keep this private and use the underscore as a convention and then in our awake notice as I start typing it's already gonna start Auto completing its found our variable there and it knows it's there but egg type I and and kind of filter it down even more and then just hit enter it's going to give me the initial position what we're gonna do is set this to a value so I'll say equals and then we'll use that built-in transform component so you transform with the lowercase T dot position so when our bird first starts up we're just gonna take the position that it's at and save it off into this variable so that we can reuse it again we're gonna be moving the bird around so this transform position won't be in that initial position ever again unless we've forced it back to that position so this also gives us a nice place to reset the bird and say we want to reset our bird after we've launched him and he's died and we want to reuse him we just drop him right back to the initial position so we've got our initial position saved off and now we're gonna go down to on a mouse up because in our on Mouse up that's when we want to launch the bird right so we pull him back we release the mouse and he goes flying this also by the way applies to fingers we could pull him with our finger and release and he'll go launching counts just like a mouse does so to make him go flying we need to access the rigidbody component so just like we got the sprite renderer we can say get component and then we say rigidbody 2d we have to use the 2d one open and close parentheses and this is again just gonna return that rigidbody component to me and then it's gonna let me by putting a dot at the end just do something with it so I'll say add force right there the first option when I type a B then we do open parentheses and add force takes in a vector it us to give a direction and a magnitude so I can say add force in the upward direction which would be a vector that only has a value on the y-axis and has zero on the X if I did one just on the X I'd be moving horizontally if I do one on the x and y then I'm going whichever direction the X and the y are facing and just think of it kind of like a graph or a line graph with the you know the X and the y hopefully that makes sense but we're gonna add force in the direction to the initial position so we want to figure out the direction to this position and add force in that direction so we need the direction we don't have it yet so what I like to do this is type direction to initial position and then I'll add a semicolon at the end and direction to initial position doesn't exist yet so we're gonna create it there are a couple ways that I can create this I can just add a line right up here and start typing it out say vector 3 and it direction to initial position equals whatever well I can hit alt in tirana so I can select it and hit alt enter and then choose the generate local option or it's actually a vector - I said vector 3 didn't I so generating really helps so this gives me a vector 2 which is just a 2d 2-dimensional vector so we don't have that Z value and right now it's setting it to the default value that's not what I want I want it to be the difference between our initial positions so I say initial position - our current position to get our current position that's the transform dot position so now we're getting the direction to that position and this isn't just the direction by the way this is the total distance or the total movement amount so if we've moved over 10 on the x axis we drug them way over to the left and we move down 3 on the y axis we're gonna get a whatever it is minus 3 and minus 10 so we'll have minus 10 on the X and minus 3 there or we should be getting the inverse of that actually so what we're gonna do now is add force in that direction to the position so let's save this off and go back into unity play and we'll give him a launch and see what he does so we drag and we release and you might see it he's slowly kind of moving up right some kind of see it in the direction let's stop playing and try it one more time so now I'm gonna take him up this way and pull them really far and let go and you can see he's kind of moving down this other direction so he's moving towards the initial position but he's not going very fast I want to speed this up so I want to take the amount that I've moved him and multiply that before we apply the force so we'll go back into our bird script and what we're gonna do is just I'm gonna add another line here after 21 and we'll zoom meant I need to zoom in here I'm gonna take this direction to initial position and I'm going to multiply it so to multiply we just add a star which is shift 8 and then we'll multiply it by a value let's just say to multiply it by 100 now so he's gonna move 100 times faster than he just did I'll minimize this go back in and hit play and pull him down and release and look at that it's getting closer right he's flying off but he's also not falling so he goes flying but he doesn't do that whole arc where gravity is pulling him down and remember that's because we set the gravity scale to zero so how do we fix that well we can go back into our script and we can just tell our rigidbody to set our gravity back to 1 once we release so we'll get our rigidbody again and then we'll say gravity scale equals 1 so now we're setting that gravity scale back up to 1 by the way this is a little bit of an issue and we're gonna talk about this in a little bit we don't really want to keep getting the rigidbody constantly but it's not a huge deal right here because we're only doing it whenever we launch if you see a lot of stuff about calling getcomponent in a slow calling getcomponent can be bad that's true but when we're just in a mouse up situation like this and we're keeping it simple it's not gonna make any difference at all so don't worry too much about that just know that we're getting the rigidbody we're telling you to add force and then we're getting it again and telling it to set our gravity scale back to 1 so let's go back to unity try one more time grab our bird we release him and look at that he goes flying and rolling I grab my bird and release him and he got all weird and buggy because he's not actually resetting let's try it one more time then see if I can launch him really far I'm just gonna pull down like this and launch him up Oh still doesn't go very far now let's talk briefly about resetting our bird say we've launched our bird way too high off the screen or way too low or maybe let's go to our scene view and switch back to 2d maybe our bird has flown grab him and he's way over here somewhere or down here it's going to gone off the screen we want our level to reset in our bird to reset or maybe our Birds even just sitting there kind of floating rolling on the ground and he stopped and he's hung out after we've launched him we want to be able to reset and relaunch our bird so I'm gonna move him back into position and then what we're gonna do is go back to our bird script and write another little bit of code so we'll go into our bird and right after the awake we're gonna add in one more special method and this is a method that you're gonna use all throughout your programming career at least while you're in unity and that's the update method so I'm gonna start typing yupi and see update automatically appear and hit enter to fill it in so the update method this is the one that was commented initially that we cleared out and it says it's called once per frame again this means that if we're running at 100 frames a second this is gonna get called 100 times a second if we're running really really slow on some super old piece of hardware in one frame a second we're gonna get this once per second or for under a frame a second we'll get it even more rare usually though we should be targeting around thirty to sixty calls to our update every second running at just 60 frames a second it's pretty standard so I would expect that this is getting called a lot especially in our current scenario where we don't have much going on so we'll need to take that into account as we build this up but not too much what we really want to do here is do a check for our position and see if our position every frame has gotten outside of the acceptable bounds so to do that we'll just write a simple check we'll say if and an if statement is just gonna do something if the thing in the brackets that we're about to put or the parentheses that we're about to put here is true so we'll say something like if and I'll say transform dot position dot y is greater than ten so if the position the Y position is greater than ten we're gonna do the line of code on the next or the code on the next line so to do that there the next code will be seam manager and as I type this it's just gonna be red so I'm not going to get any intellisense the reason for that is that our scene managed hasn't been added in with our using statements so if I go up to the top we're only using unity engine not unity engine scene management so if I click off of this move my cursor and I hit all to enter I'll get an option to add a using unity engine dot scene management statement up to the top so I can just click on this and it's gonna add it right in and scene managers gonna turn a light blue suddenly it's accessible and I can use it so now if I hit dot I'm gonna get some options here there's a get active scene I get seen at get scene my name get scene by path we're just gonna say well let's keep looking for it load scene we're just gonna reload our current scene so with a load scene and then open parenthesis and load scene wants either a build index number which is essentially the order that you've added the scenes to the project and it's not how you've saved them there's actually a scene management system that we'll use later where you can add seams and we can reference them by index or alternatively we can use the scene name so what we want to do is just reload the current scene and we can actually get the current scene with scene manager dot and look at that get active scene so I'm just gonna go down hit enter and then it's gonna want an open closed parenthesis and then we'll say dot name so we're getting the active scenes name and then we're loading it in fact let's split this out and let's change the way our if statement looks a little bit so I'm gonna zoom in just a little bit more here and with an if statement we have two options we can either run the one line of code after it or we can put the little braces here and it'll run everything inside the braces now I've added the braces but my scene manager load scene chunk is no longer inside those braces so it will actually run every update and not just whenever we do the position check or whenever we pass that position check that our Y is too high so we're actually going to take this and cut it so just control X and then I'll click right in here on line 17 and ctrl V to move it I could have also alternatively just moved the brace so that it encompasses that butt doesn't matter either way so now we've got this little bit of extra space here and why did I do this well it's because I want to take this scene manage or get active scene name and I want to cut that as well so I hit ctrl X and then I'm gonna go up one line right above 17 and I'll say string current scene name equals and then I'll paste so we're gonna get the current scene name into this string and then I'll copy that with ctrl C so double click to select it ctrl C to copy it and I'll click right here and ctrl V to paste so we're gonna get the scene name into this string variable and then we're gonna say hey load that scene it works either way I just wanted to make it a little bit more explicit and write it more how I would write it in the real world so that it's very easy to tell what's going on and it's easier to read and go through so I'm gonna get rid of this extra line here on line 20 with shift delete and save this off and then we'll go play it and see if our transform position dot Y value so our Y position gets greater than 10 we should see our level just reload let's try it out so hit play and then grab the bird and launch him and it's not going very high let's select the bird real quick and look at his Y position in fact if I drag him all the way up to the top I still can't get him up to above 10 so I need to force him up above ken and I'm gonna take my game view in my scene view side by side just click on him and drag him and watch his Y value go up and when it hits 10 you should see the entire scene just reloads yep there we go everything just reloaded so let's change it real quick so that we can launch him that high and if you remember when we drag him down and release he's going in the direction towards our start position times some multiplier and right now I set that multiplier to 100 you may have cranked yours up already but if not let's go in there and turn that value up so to scroll down here to this multiplying part on our add force and let's change this from 100 to like oh I'm gonna go with 600 so we're gonna add a lot more force 6 times the force that we were with 100 we'll hit play again will launch him up and I'm gonna select the bird I'm gonna lock click and drag and launch him and he goes fly-in and look at that it reset this time so let's do it one more time click drag and he goes flying and it resets notice that I'm losing the selection of the bird and that's because the whole level is getting reloaded and the bird that was there at that instance of it doesn't exist anymore it's a new version of the bird so we don't have it selected in the inspector anymore but now I think you can see that we're actually able to launch you also might notice that launch value is really really high so let's talk about how we can turn that down and make it adjustable and then we'll go in and fix all of the other cases where our bird could get out of range or sit down too long so we'll go back to our bird script and what we want to do is change this 600 value into a variable something that we can modify easily without changing code constantly so we can change this up while we're running and while we're playing just doing it live and not have to worry about recompiling or going back and forth to code so to do that we're gonna introduce a new variable here in replacement of this 600 and since it's going to be a private field we're gonna use that same underscore casing or the underscore convention so I'm gonna select the 600 and I'm going to say underscore and I'll say launch power just like that now I'm gonna hit escape and it doesn't exist yet so I'm gonna actually hit alt enter and allow it to generate a field which is the first option again we can go up and type these things but I really prefer to just keep it simple and use the auto generator as much as possible plus I want to show how the auto generator is going to get it wrong here so I'm gonna hit enter and it created a field but it didn't go to it so I can't see the field I could scroll around and look for it kind of go up look I can see it up here but I also wanted to show you that you can also hit f12 with the mouse cursor on it or with the carrot on it I can hit f12 and it'll go right to my launch power variable now there are a couple things wrong with this one I don't want it to be a vector - I don't want to change the power upward versus the power to the right I want it to just be a single value with a vector - here if I put 2 for the X and 1 for the Y it's gonna go twice as fast to the right and only whatever the normal speed is up and I really don't want any of those I want numbers more like 600 or five hundred or something so I'm gonna change this instead of being a vector2 to being a float and the way this is gonna work if I scroll down here because we're multiplying a vector Q times a float it's actually just gonna multiply the x value and the y value times this single value a float is just a floating point number it can be you know anywhere from a very very small negative number or very I guess big a negative number to a very big positive number with a lot of decimal points in there so you can have you know one point zero zero zero zero three or we could have a three three million in there we can have a lot a big variety of numbers and a float is usually what we use for most values and game development sometimes we use other things for different scenarios but a float is kind of the default so we've added this private float here and remember I said that we could add the private accessor there and it would be exactly the same and that's actually the way that I like to do it and it's mostly just so that they line up and that they're easier to read and it's very obvious that this is just a private field so we've got the private key word there on both of them now you don't need it you could remove it if you want but I would recommend keeping it and then we need to give it a value so our initial position gets a value in awake another thing we can do with variables is give them a value when they're created or defined so we've got this launch power and I'm going to give it a value of 500 and then what I'm also going to do though is go to the beginning of this line and I'm gonna add a square brace which is the key right next to P then I'm going to type in serialize filled with capital S and the capital F and then add a closing brass brace that square brace right there and then a space in fact I'm actually gonna add another line here so this is on a totally separate line this serialized field attribute is gonna do something special so go ahead and hit save remember file save or ctrl s and then we'll go back into unity and let's take a look at our bird again you're gonna notice something different about him if you look at the bird script we now have a launch power variable that we can modify so now I can adjust this launch power I can click and just drag left and right right go ahead and type in a number like 350 and I can just adjust this value in the inspector without needing to go back and change the code the 500 that I typed into the code will be the default so if I add a new bird it's gonna just default to 500 but I can change the value to whatever I want so I'm at 350 now and I can launch him and see if I hey that's pretty good it might be a little bit too strong though so I'm gonna stop and maybe turn it down to 250 hit play again and relaunch him and now there we go I've got decent power and I think that's probably an okay value of course you can adjust this and tweak it as we go along and just keep changing it you can also make just about any variable public like this or not really public but serialized so that it shows up in the inspector and can be modified now most of the time we do this for things that are gonna be modified by the game designers not so much the programmers programmers tend to like to hard-code the values or set in the values that are gonna make sense and not need to customize them later but when we have design data like this like how far does this bird launch we want to make sure that it's customizable by design so they don't need to go into the code and change it to make their adjustments also we might want a variety of different birds that fly at different powers and we want to be able to customize it on the bird without writing a separate script for each one now let's make him reset if he flies too far to the right or too far to the left to do that we can go into our if statement here in our update and add in another check so one way you could do this is to copy this and then paste it and say hey if the exposition is greater than zero then do this if we could paste it and say hey if the exposition is maybe less than negative 10 so we've gone 10 to the left then do the same thing reload the current scene and we could say hey let's paste it one more time I'll paste it right up here if the Y value is less than and let's just say negative 10 so say we went anywhere too far to the right or too far to the left let's do a reset this is actually gonna work let's go try it and then let's talk about how we can fix that because it's a little bit ugly and we've got a lot of code in there doing the same so I've got it there and I dragged him over this way and he goes flying to the left and at recess I grab him here it goes flying to the right Oh didn't even do it go flying to the right and eventually he gets far enough over he resets and I think if I can get him to just drop down below let's see is it possible to get him to launch so there just drops down off the edge no he's still always resets well if I take him and I just drag and we're really low down below negative Ken we can see everything resets again so it all works but how can we clean this up and make it so that our code is a little bit easier to understand and not a copy-paste mess here we have a couple options first I'm going to delete all of these duplicated transformed opposition checks and all of this code so I'm going to go right here select it all and hit delete so it's just shift at the beginning click to the end and hit delete get our update back to looking just like this remember to shift delete to delete a complete line like I just did so another option we have is we can add in an or statement to our if so we can say if the positions Y value is greater than 10 and then I can do space and I can do double pipes so this is shift and the key right above enter on most keyboards it's the one where your backslash is so if you have a weird keyboard and your keys in the wrong spot I'm sorry you'll have to find it but it's this little pipe so you hit it twice so let's say if the position dot Y is greater than 10 and then this is or this little chunk of code means or and then what I usually do is put these on the next line so just say enter transform dot position dot Y is less than negative 10 then we can say again or so double pipes transform dot position dot X is greater than 10 then double pipes again or transform dot position dot X is less than negative 10 so now we're checking to see if we've gone outside any of those areas or if any of those values have gone outside those acceptable ranges and if so if any one of those is true we'll load our scene again so I'll save we'll go back in we'll play one more time and we should see the same exact behavior but with a lot less code so poll we drag your launches and it resets you pull we drag and launch and well not resetting there so we need to address that situation but let's try that launch to the left okay so let's talk about the situation where our bird just sits on the ground how are we going to address that well we'll go back into that same chunk of code and we just want to set up another way to reload our scene right or another condition for why we should reload so to do that we're actually gonna implement a little bit of a timer what we want to do is say once we've launched our bird and our bird has stopped moving we want to start counting some time and if that time that he stops moving passes maybe like a second or two so he's been sitting there kind of not moving at all for two maybe even three seconds then we'll do a reload let's say hey okay we've obviously not beaten the level we just kind of landed and crashed so let's reload the level so to do that we're going to need to keep track of a couple things first we want to know whether or not we've launched our bird to know if we've launched our bird we can actually just look at this gravity scale here so our gravity scale is one when we've launched our bird and it's zero before that right we can also add in a boolean parameter or a boolean field here that says hey we have launched our bird and that's what we're gonna do so we're gonna go up to the top here and right below this initial position we're gonna say private bool and now a bull can just be true or false it's just a way for checking something like an on or off state or whether something has happened or not happened it's just a true or false statement so we'll say bool bird was launched and a semicolon now the default for a boolean value is always false so our bird was launched will be false at first and we'll copy this some just double-click and select it and ctrl C to copy it and when we do the on mouse up and we do this launching of the bird right after we set the gravity scale a hit enter we'll say bird was launched equals true so now we know that our bird was launched and our counter or our timer for hey have you been sitting down too long can start counting now how does that work well we'll go up to our update and after we've done this check to see if we're outside of the position actually you know what before we've done it before we do the check to see if we're outside the position we'll add in another a little bit of code so I added two lines right here after the first brace of our update and we'll say if underscore bird was launched so this will only happen if this is true and then we're gonna use an and statement so we'll say and which is kind of like or but requires both of them to be true which is just gonna be shift seven shift seven give us the double ampersands and we'd need the two of them if you only do one you're doing something different you know the two ampersands there so we'll say if the bird was launched and getcomponent and then we need to put in the flesh the inside and do rigidbody2d I spelled that wrong RI GID body to D and then we'll say open close parenthesis dot velocity so what we're doing here is we're getting the rigidbody velocity or the rigidbody component and then reading the velocity the velocity is a vector - it's going to tell us how fast it's moving to the left and right along the x axis and how fast it's moving on the y axis but it also has a dot magnitude option this will tell us how fast it's moving combined so it's gonna take the X&Y movement and the and combine them together and give us the one magnitude so say if the magnitude is less than or equal to zero should never be less than zero but we'll just put in the check they're less than or equal to zero actually let's do less than or equal to zero point one so if it's less than point one so we're very slowly moving maybe we're just kind of like teetering back and forth or slowly sliding along something like that if we're at that point where we're going that slow and our bird was launched then we're gonna add in another brace here and we'll say time sitting around that's just like our timer so how long that's been sitting there waiting to reset or not doing anything and when I say plus equals and this is going to increment it by some amount and we're gonna use a special variable here called I'm with the capital T dot Delta time I'm gonna put a semicolon at the end we'll talk about what this is and then we'll add this so that it stops being read so time.deltatime is the amount of time since our last frame remember I said update gets called once per frame so if we're running at one frame per second time dot Delta time is gonna be one because it's in seconds if we're running at two frames a second time dot Delta time is gonna be 0.5 or half a second for running at four frames a second it's gonna be 0.25 if we're running at 60 frames a second it's gonna be 0.16 and you can figure this out just by dividing one by the number that you get your frame rate is so if we go like calc let me pull up my calculator real quick and we do 1 divided by 60 you see that this is the what we should expect for time.deltatime assuming that we're running at 60 frames a second that exact frame now this is gonna vary dramatically from frame to frame just based on the system that you're on we could be running at 100 frames a second you know it's bouncing between a hundred and a hundred and ten so don't expect this value to be static or anything it's meant to be used to well really take into account time and use that to smooth things or just account forward in our code so again if we run it like four frames a second we do 1/4 we'd have a point two five or other words Y is known as 250 milliseconds in our other case we're at 16 milliseconds at 60 frames a second let's close this out and let's talk about our time sitting around so our time sitting around doesn't exist and I'm gonna use the same shortcut that I did before I select it I'm gonna hit alt enter and I'm gonna generate a field and look if you look here you can actually see it's gonna do a float so I'm gonna hit enter and we got a private float time sitting around now I'm gonna take this and I'm gonna move it so I'm going to select it shift at the beginning and end to select the whole thing then shift or control X to cut go up here and ctrl V to paste and hit enter the only reason for that is that I want to keep my serialized field a little bit separated only for readability it doesn't make any technical difference but I like to keep things as readable as possible because I have to read the code constantly and if I'm it's easier for myself it just makes my whole life a lot easier I guess and it makes just building projects easier so we've got our time sitting around is now incrementing if our bird was launched and our velocity is below zero so remember when we first launched our velocity is gonna be some crazy number not until we land and just sit there does that timer start counting up so our time sitting around is gonna go up and then we can just check here in our if statement we can say if any of those positions is the case or if time sitting around let's go select it is greater than three so if we've sat around for more than three seconds or we went out of the scene then okay go ahead and reload us so let's save that and then we'll go back into unity and we'll hit play and I'm gonna show you a little trick here where we can actually see what that time sitting around value is so let's go look at our green bird and right now we can't see time sitting around because it's not a serialized field you might think hey I don't want to see what that is in you know maybe I'll make it serialize so that it shows up that would work but we don't really want to do it that way just because we don't want to be editing that value in the inspector so what we can do is choose this little option right up here and oh it goes off the screen so just click select it and choose the debug option the debug option and this can get confusing make sure that you remember that you've turned this on because it's gonna make every component look very weird it's gonna show all kinds of extra info about them but if we look at our bird you can also see it's going to show that time sitting around so now if I grab my bird and I drag him and I just kind of launch him look at the time sitting around starts to count up and then he resets if I do that again let's just drop him on the ground oh I gotta go reselect them oh okay here let's try that once more so I'll go select my bird I'd go find the bird script watch that time sitting around by your mirror doesn't do anything until he's been launched and is not moving very fast then it starts counting up and then resets again you can turn that back off and you definitely should turn that back off by clicking here and going back to normal mode it's really nice for being able to view those private fields but it also ruins a lot of custom Specter stuff and makes the editor in general much harder to work with it's only there for actually debugging and looking at things that aren't meant to be public so there we go turned it back off and now we should be able to launch our bird off in any direction he resets and if he just lands on the ground and sits around he resets now I want to add some fun by giving us something to hit so let's go back to open game art and search for a crate now I want to point out that some of these crate options aren't actually going to work obviously the 3d ones won't work but also if you click on some of these and you see the SVG extension those aren't going to work by default either SVG is a scalable vector format it doesn't work by default in our game engine we have to do some extra stuff and we're gonna avoid those for now instead we want to find one that uses a PNG so I'm gonna go through and I believe this pixel wooden one is a PNG yep so I'm gonna grab this file just click on it and download it put it into my downloads folder and then I'll pull it into my unity project so go back into the unity project and we'll just take it from right here and drop it in I'll go select that crate again and make sure that the texture type is on sprite and that looks good so we've got a 256 by 256 sprite of our little crate now we want to add our crate into the scene I'm in the game view right now not the scene view so I'll switch to the scene view right click and drag to get over to where I want to put it I think I want to stack up these crates right about here and I'll take the first crate and just drop it out and look at that I have a crate but I can't see it the reason for that is my order in layer remember the order in layer matters here and our background is at zero so anything that's not above zero might show at the same level or behind it so if I drag that up to a 1 I'm good to go another option I have by the way is going to my full background and I'm gonna set this to negative 1 now if I go back to this crate and I set the order in layer 2 0 or I pull in something new it's not gonna be necessarily behind the background I'm gonna set the crate to 1 though just in case now our crate is definitely too big our little bird shouldn't be flying up against these giant crates so I'm gonna scale it down to do that I'm gonna hit T which is gonna give me this nice little rect tool it's this one right here so if T is the hot key selects this thing right here and now you can see I can grab it and drag in all different ways and resize it I can even make the scale negative look at those values you're doing negative I don't usually want to go negative though said what I want to do is hold shift while I'm dragging this around and make it so that the values locked together so our x y&z are all gonna change the same it's gonna keep it at the same aspect ratio then I want to get down to about the size that I want I'm thinking realistically I think about 0.5 is good once I've got it close I just go in here and just type in 0.5 tab 0.5 tab and then a 1 for the Z because the Z doesn't matter okay so we've got our box right here about half the normal size and then I want to make it so that our box can fall and get knocked over so remember there are two important components here we need the rigidbody for falling and we need the collider for handling hitting things and actually landing otherwise it's just gonna keep falling forever so click Add component clear out my bird search and go to physics 2d and find the box Collider 2d and look here it just made a green outline around the box if I toggle it on and off you can see that green outline kind of disappearing type let's switch to the move tool now it makes it a little bit easier that rect tool kind of hides the green outline hopefully you can see that just going on and off got the box Collider there and then let's add a rigidbody component so hit a cone this time I'm just gonna type RI GI and have it filter down all right GI and I'm choose the 2d option so now if I save my scene control s and hit play I should see that crate appear and I should even be able to hit it so if I pull my bird back and release look at that he can knock the crate around so I'm gonna stop playing and I'm gonna do something important here I'm gonna go select my crate and I'm gonna hit f2 and rename it to crate I said it's important it's not super important but I think it's good to just keep things named well so we've got our crate there and I want to make another crate I want to stack these things up so that our bird can fly into them and knock them over so what do I do do I just take another crate drop it out and go through that process again of course not there's a much simpler way to do it what we'll do is select the crate right-click on it and choose duplicate now I have a crate with a little one next to it this is a second crate in the exact same position so it looks like it's on top of it on the move tool again so it's W or click off here I can just click the green arrow and drag it up and you can see I've got two of them I can do the same thing again just right click and hit duplicate and grab and pull it up there's also by the way a hotkey so if I select great too and I hit ctrl or command + D for duplicate it'll do the same exact thing and I can drop up another crate and I've got four crates all slightly above each other and remember these things are just gonna fall down and land on top of each other because they have a rigidbody they have a box collider and they have a gravity scale of one so they're just gonna follow normal gravity and fall right down let's hit play and watch that in action there we go the boxes are sitting on top of each other and if I pull my bird back and launch him you can see I can just knock them out of the way look at how easy that is before we build the next feature I wanted to show you again what it looks like so let's grab our bird and pull him back this is in the final version of the project and notice that little set of arrows you might already know what they're pointing to think about it for just a moment remember that start position that we cached these arrows are actually just pointing directly from our bird to our start position and as soon as I release they go away so let's talk about how that's done and how we can make it happen in our project first I'll stop and get that out of the way and then we'll stop playing in our main project and let's go take a look at our green bird I'm gonna zoom out my scene view so that he's visible here by the way I can also with my mouse over the scene hit the F key to focus him but then I gotta zoom back out because it makes him kind of a highlight there so let's collapse down the rigid body and the sprite renderer and all of the rest of these components because we're gonna add in a new component called a line renderer so if you click the add component button and you start typing Li and E you should see the line renderer option appear so click on that and a line renderer is a special tool for drawing exactly what it says lines now these can be straight lines or they can be crooked or curved or whatever we want but it's essentially just a bunch of straight lines combined together with the different points to make whatever shape that we want we're just gonna do a straight line from one point to another though we want to go from our bird to that Center or starting point so let's look at how a line renderer works it has a couple options here there's a loop checkbox and a tolerance we don't need to worry about those there's also this width slider which you can't see anything yet because there's no line drawing so we can't see it adjusting and then there's a color option and a material option so we can change out the way that our line looks but first we need to expand out positions our positions is actually what's called an array it's a set of data points or a set of vector threes notice we have XY and Z and it's set to a size of two by default but also by default the starting point and the ending point of our line are exactly the same except for the Z value so our line renderer is trying to push backwards into the which doesn't work really well in the 2d game so we're actually gonna change the Z to be a zero and then let's just change this x value and crank it up so if I click here and just drag it over and set it to like a twenty and you notice that we still don't see anything but if i zoom out a bit look at that you can see this weird purple line sticking out of the right so there's a couple things going on here first is that our starting position is right here behind our background so if I actually go select my background and uncheck it to disable it we can see the line is going from this zero point over to 20 but it's not rendering on top of my foreground and it's definitely not in the right place so first let's fix that rendering in front of the background thing so if we go select our green bird and we look for the line renderer and scroll down we have this materials section material is just a combination of what's called a shader and textures and other textured data or settings data that tell the engine how to render our objects or how to render the different things that our materials are assigned to by default it's on this none material which is that bright purple you may have seen this in games when things bug out something's missing you get this weird bright purple texture it's so that it's really obvious hey something's wrong go fix it in our case that's exactly what's going on we have this bright purple texture saying hey you haven't given me a material yet you should probably go do that so if we go to the materials section and this could be collapsed by the way and just expand it out we want to leave the size at 1 just like it is because our line renderer only needs this one material and then we can click the little search button here this is gonna bring us to all of our different materials in the project there's one right here called sprites default by the way if you're in the list mode it's just this little slider to go back and forth so you can grab it and drag the list mute mode or this big preview mode we can go right in between I'll go to list mode now well choose sprites default and look at that our sprite now or our line renderer using the sprite material is now drawing on top of the background it's now a sprite so it's drawing kind of in the right way before I was doing a 3d version of it we're using the material made for 3d rendering that doesn't work well with our 2d sprite background so we've changed that and now we've got the line somewhat in the right well at least visible but it's not in the right position right we want the x-value to be really kind of like lined up with our bird so you like drag it down here grab the Y and drag it down now you can see like it's going to our guy to way over here it's still totally wrong right like we don't need this we are lieing going off into the middle of nowhere but first I wanted to grab the width and show you how we can adjust that so if we grab this little piece right here and just drag it down you can see we can adjust how wide this line renderer is go all the way up to one or all the way down to like a tiny little line so if you just want to do like a flat little line and you don't want to put a texture in there you can make it nice and skinny maybe out of color to it or something we're gonna leave it wide though because what we're gonna do is add a texture to it we're gonna do that after we get it hooked up in code though so now that we've got our line renderer set up let's go into the code and actually make it go to the correct positions so the ending piece is on our start position and the starting piece is wherever our mouse or our bird is so when we drag it apart the line goes from that start to the bird so we'll do that by going back into our bird script and in our update method we're just gonna add in another check or another bit of code to update our line renderer but first we need to get our line renderer our line renderer is a component so let's say at the beginning of our update wouldn't say get component line renderer and we use the open close parentheses dot positions so if we look at our or an action at position step position so a set position and then we have an index and a position so we have two different indexes here let's go take a look at that we have index 0 so that's this first position and then we have index 1 which is our second position so we're going to set this position right here to just be our starting position and then we'll set this one to be our birds position so we'll set index 0 to be the starting and index 1 to be the birds current position let's go back to the code so say index zero and we said we'll make that the initial position and put a semicolon then I can copy this line so at the end I just hold shift go home and then ctrl C hit end enter ctrl V just copied and pasted my line but I'm gonna set position one here instead of zero and we'll set it to the transform dot position so this is gonna make the line go from my bird to that starting point so let's save that off we'll go back into unity press play and let's watch our line renderer so now it goes from our bird to that starting point just like we wanted it doesn't quite look right it doesn't look like arrows but we've got an idea of the direction that we're facing at least and it also kind of stays after we launch so we need to address both of those things we need to give it a texture so that it looks right we also need to make it not show up after the bird has launched so let's stop playing again and first let's fix the code part where we're making it not show up after we launch so we'll go right back into our code and then to do that we can just say in our Mouse down we'll turn our line renderer on so say get component line renderer dot enabled equals true and then we'll do a copy of that line so again shift and home to go right from my cursor to the beginning control C then we'll go to the end of on Mouse up hit enter 2 times paste and we'll set the align render and able to false so here we're enabling the line renderer when we start to drag and then we're releasing it when we stop dragging so we'll save that off go back into unity one more time make sure that that works and then we'll talk about how to change this line into pretty arrows so there we go it works there and I release and it goes away closer to what we want but we still need the arrow art now believe it or not the beautiful arrow that you saw was actually something that I made and I'm gonna show you how to make it or how to grab your own arrows on the internet or pick whatever you like but I want to show you the real quick process of how I made it so I'm using an open source free tool I don't know if it's open source but a free tool called it's a new image something something it's a Photoshop alternative essentially it allows you to draw art and do a lot of the things that Photoshop does and here you can see my arrow that I've drawn to do this all I did was go to file new and then I chose a width and a height that I thought would be acceptable 128 by 64 seemed okay and then went to Advanced Options this is a very important part the fill with area needs to not be background color but transparency then I hit okay zoom in with control in my mouse wheel and then I find the align tool by the way I am terrible with doing this art so if you struggle with it don't worry you're not the only one or it's the pencil tool actually then turn my size way down to like a 1 let's see I'm going there and just type in a 1 and then I click here hold shift oh whoops undo that go to the white color so I can get a nice white line then I click hold shift oh nope undo that so hold shift click there hold shift there we go and then click over to you I don't know about here right around the middle and then hold holding shift click again to the middle and you can see that I came up with a line that's way too narrow so I'm gonna undo that with control Z go back to white because apparently I switched off of that again and then turn up the size a little bit more so go to like a 4 pixel wide one click hold shift click and click and now I've got a very basic little arrow so to save it off I go to file and then we want to choose export as we want to put this into a folder that we can find it in so I'm going to do it in the Downloads folder and it'll just name this white arrow and we hit export and this will show up in my downloads folder once I hit this last export button it does pop up a lot of dialogs so go to my downloads folder alright it actually went to my documents /downloads folder I had to repopulate document / download it's important to know where your files are being saved so I find my download of my or my white arrow that I've just drawn and we're gonna drop this into the project now I want to also point out that while you're going along you could just search on Google for a transparent arrow and pull that in but if you do you need to make sure that it's actually a transparent image sometimes when you just right-click on these and do a save image as it's gonna save off a jpg or something else that's not transparent and doesn't work and you're gonna get weird looking things that you know you have a checkered background instead of a transparent background so whenever you're looking for transparent files make sure that you also go to tools color and transparent if you're searching that way you can find things with actual transparency not just things that have the word transparent in the search results okay I'm gonna go with the arrow that I made though because I think it's nice and pretty and I'm gonna make it available as a download just look below where you're watching this and you should see a way to download this arrow and pull it into your project so I've got my white arrow and I want to replace this line renderers material with my white arrow right now I have it set to the sprites default so that I can see it that's not really what I want I want to create my own custom material my own material is gonna allow me to customize the way that this arrow shows up and well not a whole lot more than that but theoretically we could do quite a bit more with it in a more advanced scenario so let's set up our material to do that we go to our assets folder and which right-click choose create and then look for material I'm gonna call this white arrow and if you hit enter don't worry you can just hit f2 to rename it and go back and change the name and then we're going to assign the albedo of this shader or this material to do that we can click on the little circle here and get a pop up of all of them and we can go look for our white arrow we could also alternatively just drop it in and this is the texture that shows what our thing looks like it has all of the color and really the drawing data the primary data of any material is going to be right here now we need to make one other change though well first let's actually assign our material and then we'll make the change so we'll go select our green bird and right where we had put the sprites default before let's put white arrow to do that I'm just gonna drag it and drop it in again you can also hit this little search box and have it pop up and go double click it to pick it but either way works and I want to show both of those options but notice that I'm still not seeing my line renderer if i zoom out and kind of see a little bit of what's going on there but it's not looking right now I need to make one more change to this material so I go select the white arrow material again and we're gonna change this from standard opaque and we're gonna switch it over to be transparent now when I switch to transparent notice that it starts to show up that's closer to what I want right I want to be able to see the object here but I don't necessarily want this transparency showing up so if I change it to cut out well that doesn't quite work either so what do I want to go with well I want to look for instead of the standard shader I'm gonna switch over to an unlit shader so I'll go from standard click the little pop-up and then go to unlit and then here we'll choose transparent so now look at that we've got this white little arrow and the main difference here is that when we're using that standard shader it's gonna take into account lighting of our scene we don't really have any lighting so it's just gonna be dark if we use unlit it's just gonna match exactly with the texture color which is that solid waste now we have this one white arrow it's not going all the way out to there though it's not repeating so let's fix that too to make it repeatable we actually need to change something on the texture here that's this little version of the white arrow the one that shows the picture let's go to the zoomed in mode or the bigger preview mode to annainsea it better so the material has this kind of weird rounded thing it's actually because it's showing what the material would look like on a sphere so if we dropped this on a sphere this weird arrow it would look kind of strange and wiggly and distorted if we look at just the texture though we can kind of make out the white line there on top of the back the background that's transparent so we're going to select this texture and we want to change a couple of things first we need to change the mesh type from tight to full rekt which just means that it's not gonna it's going to use our entire texture and not try to shrink it down or cut it down and the reason for that is we want to change our wrap mode which is for repeating or like a wrap Abul texture think of something that just repeats over and over and over which is exactly what we want ours to do we want to change this to the repeat mode so we hit apply after doing that and suddenly we should see our arrows appearing all the way along now the last thing I want to do with this is go select the green bird and you can see I've already kind of played with the width here I might have cheated there so we can just adjust it up and down until it looks right until it's like a good enough width for you so I go with like a point four point five something like that so we got our arrows visible and if I select off of it I can see that they look okay without the selection and save and let's just play and watch them in action see our beautiful arrows doing what they're supposed to do so grab the bird and oh no our arrows are actually pointing the wrong way they're working right I can drag out like that I can also smack this thing but I can drag them out and they work and they indicate pretty close but they're going in the wrong direction so how would we go about fixing that we need to think about it for just a moment remember if we look at our bird we've got the position zero and position one we're going from position zero to position one let's look at our code again so if we look at where we're setting our position so just kind of scroll through till we find the set positions we're going from position zero to position one it's not really what we want is it we want to go from the transform or the birds position to the initial position so we can fix this really really easily will change this zero this one to a zero and this zero 201 so now we're setting our first position or our starting position to be the bird and our second position to be the starting position or the initial position now I it doesn't matter if these are in the right order like if we set position zero before position one but I like to keep things nice and clean and easier to read so I'm going to rearrange them to do go to the end hold shift' home control X go up in the line enter up again and control V and then go get rid of that extra line with shift and delete so there we go now they're in order we could have also just moved these two swapped out the initial position with transform top position anything to get them in the right order and again the order there doesn't matter at all it's just making it easier to read because if they're out of order and you have to go look at this you might get a little bit confused when you're reading it and glossing over it so let's try that we should get our arrow facing the right direction and our bird launching off with it let's see there we go we got the arrows we pull we release and our bird goes flying great stuff now let's add some monsters that the players can kill give them a target to take out their bird rage on I guess to do that we'll go back to open game art and just search for monster and there are a bunch of these different little 2d Monster options available I'm gonna go with the blue ones this time last time I did orange once you've found the one that you like make sure that they're 2d and will download the spritesheet forum so just pull this in and then we'll open up the zip file here and go into the transparent PNG s folder again and this one actually comes with the animations for getting hit it's got a special frame for that and then it's got animations for idle frame 1 and frame 2 so I'm gonna copy these two just select them both and hit ctrl C or right click and hit copy then I'm gonna go into my project and I'm going to hit show and Explorer to pop up the folder we'll go into the assets folder and I want to drop these in but notice I already have a frame 1 and a frame 2 in my folder so I'm gonna create a subfolder to do that I can just right-click anywhere in my folder here and choose create or new but that's actually off-screen so I'll do it way up here I'll go up here and we'll hit new folder and I'm gonna call this monster the capital M notice that in our unity project if I click over we're gonna get a monster folder that appears oh it hasn't actually appeared yet well I'll show you how to get it to reappear though first let's go into the monster folder and paste so we've got the frame 1 and frame 2 in here and I click over to unity and my folder has not shown up yet so I'm wondering hey what's going on well I'm in play mode stop playing and suddenly my monster folder appears remember before I mentioned that your unity project isn't gonna update when you're in play mode as soon as you stop it'll reappear or it'll start updating again and grabbing the new file changes so here we go we've got 2 frames of a little monster in our monster sub folder then click on assets to go back to the root and you may have already put your bird into a bird sub folder feel free to do that it's always good to keep the project clean since we've had it so small though I didn't want to go around rearranging things just yet so we'll go to the monster folder again select frame 1 and hold shift and select frame two we're gonna go about that same process that we did for animating the bird will take frame 1 with them both selected notice they're both blue and I'll drop it right here oh let's just drop it right here where I want the guy to be actually it's gonna pop up asking me to create an animation I'll name it monster and it'll be defaulted as a dot annum file and again it's gonna create my animation here and my animator controller here so if I select or my controllers here so I got the words mixed up my frame 1 here is the controller that got automatically created and monster is the animation the dot annum file that I actually named and if I go up here in my scene I have frame 1 which is the monster and he's got a sprite renderer and an animator on him that animator is just set up to play the monster animation now I'm gonna rename this animator I'm gonna select it and hit f2 and name it monster and I'm even gonna rename my monster frame 1 I'll say monster frame 1 let's just call it monster - 1 and then we'll name the other one monster - 2 that way it's a little bit easier to tell what thing we're talking about so we've got monster 1 and monster two those are the two frames of him and I'll go to this one here the object that I've actually placed that's this guy right here and we'll rename this to monster now obviously he's too big he's way bigger than everything else I'm gonna scale him down - let's try 0.5 and 0.5 on the X and the y value and now he's a little bit big I think it's still a little too big so I'll go to a point 4 and a point 4 I think that looks about right put him into position behind that crate and then we'll add on the standard components so just like we added for the crate we're gonna add a rigidbody and a Collider so we'll go look for a rigidbody 2d now for the Collider remember we don't necessarily want to add a box Collider box Collider might actually work really well for this guy but we can also add a polygon Collider so if you went with a non boxy character a polygon Collider is probably gonna look quite a bit better now for some reason my polygon Collider is just not showing the render there it's still working though I can hit play and I noticed that he's not falling through but something's acting a little strange let's try adding a box Collider 2d okay that one actually is showing up so I'm not sure why that polygon Collider is not rendering normally they render fine I'm actually just gonna go with a box Collider for this guy though so I'll remove that polygon Collider and just leave the box Collider so I've got this guy here he's ready to be crushed I guess and I'm gonna duplicate him gonna select them ctrl D and make one more so got two of these guys right here standing behind the boxes ready to be smashed so I'll save this off hit play and we'll launch over at these monsters and see if we can take him out oh I was able to knock him over at least so I didn't kill him yet but I am able to hit them and knock him over but also notice what happens if I pull my bird over here too far he's going off the screen and I can't really see him and if I launch over here and I knock some of these guys over to the right oh well that time it didn't happen but a lot of the time they can just end up kind of off the screen and not really visible so I want to fix that and make sure that everything on my screen is visible at all times and we're gonna do that with a new system a new camera control system that actually makes our camera follow our bird around and show these guys all automatically without us writing any new code the camera system we'll be using is called cinema sheen it comes free with unity and we can install it through the unity package manager to get to the package manager we go to the window section and then go to package manager right here below asset store this will pop up a new window and I've already selected cinema Sheen here but you can see there are a large selection of different packages available the reason that these exists is so that unity can split up the engine into multiple smaller chunks so that when you're building a 2-d game you don't necessarily have to ship off a bunch of 3d functionality and features and your install it allows you to keep things smaller and just simplifies the entire process so to setup the cinema Sheen package first we need to find it in the package manager if you don't see it you may be in the in project filter right here so right at the top we have all packages selected but you could be in this view right here where you only see project or packages that are in your project to change that just go back to all packages look for cinema sheen or you can type it up here and search for it and then once you've found it select it and hit the install button this should only take a moment and it's gonna pull in the cinema Sheen package which gives us the full camera control system and it's also gonna give us a nice little menu option right up above as soon as it's finished now before we had cinema Sheen available we used to have to just write our own custom camera controllers for just about everything now I would say we write custom camera controllers for I don't know half the projects that I do and the other half we just use cinema sheen with a couple little settings adjusted because it works so well and it gets the job done and really saves us a lot of time so now you can see it's finished installing I can close the package manager window and I'm going to go to cinema Sheen and we have a bunch of different camera options available that we can create we're going to eventually create the 2d one but I want to talk briefly about what some of these others are the virtual camera essentially sets up a camera in our scene that can look in any place and be attached to anything it's almost like setting up an extra just camera that we can switch to so we could set up multiple virtual cameras and then tell our camera controller to switch between our different cameras and like show us a different view completely the free look one is more for a character that's moving around allows us to turn and look around the clear shot one allows us to get a clear shot of an object so we can set up multiple cameras say hey give us the best shot of this character and it'll do it automatically for us the dolly one follows along on a dolly if you've ever seen the dolly in movies it kind of holds the camera and moves it along and keeps focused on whatever the thing is that it's following and there are a lot of other options but let's just go on to creating the 2d camera so I hit create and suddenly everything broke all I see is a black screen with my little arrows on it but if we look at the settings on this B cam I've selected my V cam that got created that's the 2d camera that cinema scene made it's a cinema scene virtual camera that's what CMV cam one means one just means it's the first of them and then if I select it lo and I look at my position notice that my Z position is reset it went to zero remember our initial camera was at negative ten and if we go to our scene view and we're in QD mode or 3d mode here we can see that our camera is actually shoved up against the sprites and if I drag it out here and see it's pulling away and if I go to the game view it looks correct again now let's say I want this V cam to just follow my bird around and just keep my bird centered well to set that up all I need to do is take my green bird and drag it on to this follow section do that I'll click and hold I got to make sure that I hold the button down if I release its gonna select my bird so I just hold it and drag over to the follow field and suddenly my bird is right there in the center now if I hit play we should be able to grab our bird pull him back and launch him you might have also noticed that weird little box let's stop and hit play one more time this little box is showing up because I have my camera selected when I have this be cam selected there's actually an option I believe it's on the main camera here to show the camera frustum it's showing the area that the camera is focusing on and the what you can do is kind of set it up so that the camera won't go outside of those ranges we're not really gonna use that though but I just don't want you to get confused by it if you have v-cam showing up you can adjust this and really adjust some of these settings as you drag things around in here don't worry too much about that though it's a bit more advanced virtual camera feature that we don't need to talk about so we've got our bird here being followed and let's just go select something else so we lose that outline got our bird being followed but I said I wanted to keep the enemies on screen at the same time as well so to set that up we need to actually create a new object we need to create a tracked object or tracked group or I guess it's a target group is what it's called in here so to do that we could go create a target group camera and it would automatically create us a target group in fact I can do that right now just go create target group camera and see it made a target group one and it made this other v-cam I'm gonna delete the second v-cam and just reuse the target group now I could have alternatively just made a new game object and added a cinema sheen target group component to it because that's all this really is it's a game object just going in here right-clicking and hitting create empty would have made me an empty game object then I could go into add component find cinema sheen target group and add the target group it gives me the exact same things if I select them got the same object but I wanted to show two ways to do it and that when you use these menus a lot of the time it's just creating a game object and automatically adding components for us so I've got this target group here and it has a target list but the list is empty so I can hit the plus button and it gives me the option to add a target here what this is is a group of objects that I want my camera to focus on and keep all in the view so instead of being able to just assign a single thing in that virtual camera to follow I can assign multiple things so I'm gonna hit the plus button two more times so that I have three targets available because I have my bird and my two monsters that I want to follow then I'll take my green bird and drop him into the first target field I'll take the monster without a one behind his name and drop it into the next one and then the monster with a one which is just my second monster and drop it into this third one again click and drag don't release until you get over there now I'll go to my V cam and I'll assign this target group instead of my bird as the follow and look at that we're now focused on the and the monsters at the same time now if I hit play when I drag my bird around let's go unselect the V cam so I can see things a little bit cleaner you see that it keeps everybody on screen at once and I can pull back and release and kill them and as I get closer it kind of zooms in and gives us a much better view and the last thing I want to talk about with this camera controller is the radius option here so when we were playing and you go select target group try playing with this radius value and notice that it's gonna make us get a bigger area around the target or targets that we've set a radius for so I set a two there and maybe a five here you'll see that it's pushing these guys further to the front it's trying to keep a radius around them inside of our screen so you can play with these values and get them to something that you feel looks and acts right and then we'll start working on the enemy and making it so he can actually die correctly so for our bird to be able to kill his enemies we need to give them a script we need to actually create an enemy script this will be our second script so we'll do this right here in our assets folder we'll right click hit create and choose c-sharp script I'm not gonna hit enter yet I'm just gonna type enemy yeah Annie my with a capital e and enter because I want to make sure that my script name and the class that it gives me match now it's gonna pop up Visual Studio and go to the wrong file because that happens all the time but I can just go back in here and double click on enemy and it should open up the enemy file so now I should have a public class enemy which is also a monobehaviour because this is gonna be a component that goes on to our game object so remember it needs that : monobehaviour or else it can't be a component that goes on there it's got a start and an update and we're not going to need either of these so we're gonna cut out all of the code from line 7 to 17 so select it all with the masters drag and hit delete now I've got a public class with two braces and nothing inside it now what we want to do for our enemy is handle what's called a collision a collision is when two objects hit when our bird hits those boxes that's a collision and if we have code in there to listen for these collisions we can respond to them and do different things we want to listen to collisions on the birds or on the enemy I guess 4 hits by the bird and then make the enemy just poof and disappear and die so to do that we can use the next special method that's part of that monobehaviour say on KO lesion so you see on CO and I can already see on collision enter on collision exit on collision stay we want to do on collision enter 2d you need to again make sure that you choose the 2d version when I was preparing this I accidentally selected on collision enter without the 2d got confused said hey why isn't working and spent 10 minutes trying to figure it out only to realize I had left off the 2d because I typed it out so make sure you use the on collision intro 2d one now an on collision intro 2d is actually gonna pass us in some collision information so whenever we get collided with whenever our enemy hits anything the ground a box another enemy our bird we're going to get some info about the collision and it's going to be in this collision 2d type object named collision it's a lot of the word collision right so what we want to do is check to see if this collision was with a bird to check that we can actually read a property on our collision so we'll say if collision dot Collider this Collider here is going to be the other object that we collided with and then we'll say dot get component and we're gonna give it the type of bird and then open closed parenthesis and then we'll say not equal to null and then add some braces here and the next thing we'll say is destroy game object and add a semicolon so what we're doing here is saying hey with the other object that we collided with that Collider try to get a bird component on that object if there's not a bird object then it's gonna give us a null back so it's gonna say hey there's no bird and we're not gonna do this little bit of collision or this little bit of destruction here if it isn't null though so it means that we did get a bird back then destroy our game object in fact we could even write this a little bit different so I'm gonna take this little bit of code and hit control X and I'm going to add in a property here earth a variable here so say bool did hit bird equals and we'll put in the get component is not equal to no and then we say if did hit bird then destroy our game object so here we're just kind of representing the same bit of logic in another way that's maybe a little bit more readable another way that we could represent this too is to say bird bird equals Collider get component bird and then this would give us back the actual bird that we crashed into then we could say hey if the bird is not equal to no which means that it exists null just means that it doesn't exist it's pointing to nothing the object just is there we didn't find a bird didn't say if the bird is not equal to null then destroy the game object and this is much closer to how I would actually write my code for this so save this off and then we'll go back into unity now in unity I'm gonna select my monster and add that monster or enemy script to him in fact I'm gonna select both monsters at the same time so I've got the first one selected I'm gonna hit shift and select the second one and then I'm gonna go to add component and I've already collapsed these other components down so go to add component type in E and E my find my enemy and add it to both of them this is why I don't have to go in and add them individually but if I click on them each individually I should see that I have the enemy script on both of them now I'll hit play and see if we can kill those enemies by hitting them with our bird now I'm probably gonna miss cuz I'm not very good look at that I killed one enemy it didn't kill them both but I took one of them out see if I pull it over here and just shoot again can I knock them both out oh not quite but I also want these crates to be able to crush these enemies right oh I don't want them to just die if the bird hits them it's kind of like a weird obstacle course you might want to leave it that way though but let's say you also want them to die from boxes crushing them on the head to do that we'll go back into the enemy script and we can add in another two little check here so we want to say hey if the collision wasn't with a bird but maybe it was with a box or not an enemy then and it will hit us from the top then let's die - so how would we do that well first we'll check to see if it was an enemy so we say enemy enemy equals collision dot Collider dot getcomponent of type enemy by the way that when we put these in these little brackets here or the less-than and greater-than and whole you spell it right these are actually generics is the term for it we're actually trying to get a component of a very specific type and we're just giving it the type at compile time so that it's it's just a clean way of writing hey give me this component of this type and you're gonna see this a lot when we have lists or collections of things or we want to pass around and be specific with types of objects a lot of the time they'll be in these less-than and greater-than sections okay so we're getting the enemy and will say if enemy is not equal to no then return and what this means is exit out of this method don't do anything else just be gone you're done we don't care we don't want to do anything else with this we hit an enemy we're done with stuff we don't want to continue on so if we hit an enemy we're just not gonna do anything we're gonna bail out in fact we could even maybe have this up above our bird check I don't think it matters either way though because we're never gonna hit a bird that's also an enemy okay so we've decided hey we got to this point - line 21 - means that we didn't hit a bird and we didn't hit an enemy if we hit a bird or we hit an enemy we're going away in fact what we should do here is after we destroy we should return as well so that we exit out so if we do hit a bird we don't keep checking for other things we don't want to destroy our objects and then go looking to see hey did we hit an enemy did we hit a box do all this other stuff we're already dead just get destroyed and be done with it so what do we want to do next well now we want to check to see if we got hit on the top so if anything that wasn't an enemy or a bird crushed us on the top how would we deal with that I'm gonna say Collider or collision dot and now here's where we get into some interesting stuff the contacts section is actually an array of different contacts it's essentially a collection of collection of contacts so imagine my finger hits my hand here I've got one contact that's where my finger hits my palm right there but if I happened to hit with two things at the same time it could end up with two contacts like two contacts both it at the exact time 99% of the time we're only having one contact and in our case now we only want to deal with one contact so we'll access the contacts but we're gonna use an array index row which is these square brackets and we're gonna put it in a zero to just say that we only want to reference the first contact in here and since there's zero based indexes means the first one is number zero we want to access it at number zero if we try to do number one and there isn't a one there we're gonna get an error it's gonna blow up and we really don't care about that we just care about the single first contact it's really all we're gonna have 99% of the time then we can say dot and then we want to use the normal so the normal I don't want to dive too deep into this but it's essentially giving us the angle that we hit from and this is gonna give us an x and a y value so it's gonna tell us if it was to the left at the right and it's gonna give us essentially an x and a y value on a plot for the direction that the impact came in a so if we came in from the top it's just straight above we're gonna have I believe it's a negative 1 for the Y if we were look straight on the top for add an angle we could end up with like a 0.7 or something and then our x value is gonna go up we really just care about the Y value so we're gonna say if the Y value is less than and I'll say negative 0.5 so we want to check if that Y came in at anything that's kind of less than hopefully my weird guilty fingers make sense but if it's less than 0.5 so it's mostly on top in fact we could even say if it's less than zero or less than a different angle less than that but we're gonna go with 0.5 so if it's less than that then we want to do something but look I didn't write an if statement I put a less than and a negative and that's it so let's wrap this in an if statement so I'll go to the beginning I'm gonna go left one space here out of space say if and then open parenthesis and then go to the end with the end key and close the parenthesis so if we hit from the top with something else and do the open brackets again and we'll say destroy game object there we go so now if we get hit from the top by something else we should also die or our enemy should die at least so save that off and go in and let's go knock some crates onto these guys and see what happens look at that they both poofed and died but even though they both died I didn't get any cool puffs of smoke or clouds so let's put that in now and we're gonna talk about particle systems to get started though we need to grab a little piece of art I'm gonna use the cloud from open game art so again just searched for the word cloud on open game art and found this little cute clouds package I'm gonna open it up it's a cute cloud game ornament does it so I'll download it open it up and then let's look at these two clouds I want one of them I think it's that first cloud so the first one looks like that the second one is kind of flat that doesn't make a lot of sense for what I'm thinking so we'll go with this first one so I'm gonna copy cloud one just right click copy and then we'll go to our project folder again and we'll paste this thing in so go back to project right click show in Explorer or finder again and go into assets and then right click and we will paste to drop this in from that zip file remember if we're playing it's not going to show up automatically but once we're done playing we should see our cloud appear okay so let's talk about particle systems to create a particle system we can actually right click and choose effects particle system to just create a new one right in the hierarchy so again that's right over here in this hierarchy area and you might see a bunch of little white dots flying around I'm gonna go to the scene view and take a look at how this views so we'll go to 2d mode and let's move this particle system away from the background so I'm gonna hit W with the particle system selected zooming out with the mouse wheel I'm just gonna move it up here then I'll hit F to focus and zoom in with the mouse a bit so here you'll see I've got a bunch of weird little dots flying up that's not what we wanted all right for our cloud we want these little clouds and we want it kind of poofing out in all different directions so we need to change a couple different things here first we need to change the renderers material this is the visual thing that we're showing right now it's this default particle system that's why we see this weird little dot so what we need to do is create a new material that we can assign for our particle that uses our cloud as the so to write or create a new material we do the same thing we've done before right-click choose create and pick material will call this cloud so it's right here I've got a selected I'll just type in the word cloud and then we'll assign the cloud to it we'll switch it over to be a particle system and just have it be standard unlit so this is gonna be an unlit particle material next we'll go into our particle system again so I'll reselect the particle system scroll down and we'll assign this material see the material section right here this is where we're going to assign our new cloud material so that we can see our clouds instead of these little dots so I'll take it and we'll drop it right up there now suddenly I see my little clouds but they don't look right yet we're still missing something we still have that gray outline around and we're not getting the transparency that we expect now we need to modify the material and we can actually expand it out right here below the renderer or we can go select the material just by clicking on it but what we need to change is this rendering mode right now it's set to opaque which just means no transparency draw it and cover everything with our colors we want to actually change this to be a cutout because our particle or our cloud image is just a cutout of a part of a cloud with transparency around the edges so now it's looking I'd say a little bit closer but not perfect yet because we don't want our thing just shooting off particles like that we want particles kind of flying off in different directions and we really just want them to fly off in a big poof we don't want a non-stop like shot of particles or clouds coming out of our guys as they get hit so we're gonna make a change to the emission section here if you expand this out you see that it has a rate over time and a rate over distance and then a burst section if you crank up this rate over time let's say we set it to 100 you'll see that we're getting a lot more particles if we set it down to 1 you see that we're getting one particle a second so this is just how many particles you want it to create per second now what we want to do here is actually set this to zero because we just want to do a burst so to do a burst we just add to the burst list and then leave it I think 30 is probably a decent value so you're gonna watch it's just going to do a big burst of particles but they're not going in the right direction are they so to control the direction we expand out the shape section and look right here it's set to a cone and you can actually see that they're flying out in that cone direction exactly what we should expect here so we set a cone and if we adjust the angle we can make them fly out wider or we could pull it in and make it much tighter so they go up in almost a straight line instead of a cone though we're gonna use something different we're gonna use a sphere so if we choose sphere and watch it look at that now our particles kind of fly out in a closer direction to what we're expecting so let's zoom out a little bit and see how far they're going they look like they go really really far and just imagine we've got them right on top of our a little blue guy in fact let's move them down so they're on top of our little blue guy that's probably more than we want we want a much faster shorter life cycle of these particles we want them to kind of poof up and then fade away so we can adjust the lifetime of them by scrolling up here to the start life time section we can turn this down to maybe like 1.5 and watch again we should just get nice quick little particles they disappear much faster but I want them to fade away too so how do we make them fade well we can go through let's see scroll down here and we can look at size over lifetime so if we check this size over lifetime we can actually make them look they start to grow so watch it one more time so now they're growing over lifetime but instead what we're gonna do is click on this button and edit the curve here so instead of them going from zero to full size we're gonna make them start at full size and then go down to zero so we'll take the red if I can grab it and drag it we'll pull it down here and we'll grab this little piece right here and kind of straighten this curve out so they're gonna start off large and then just kind of shrink down so there we go we've got I think a much better particle system now there are also options for rotation if you want to make them spin go ahead and play with those if you're gonna make him change color go ahead and play with those there's a lot of different options here and a lot that you can do with particle systems and there are a lot of fun to play with you can also grab particle systems on the asset store so if you've looked at the asset store tab before you may have noticed that there are a ton of particle system options on here if you decide to use one of the particle systems that are on here I don't know if I should pull them up but if you do just note that a lot of the time the particle system prefabs that you'll get the objects that have the particle system set up will have scripts on them and those scripts might do something that you're not expecting so if you're seeing weird behavior take a look and see if there are scripts that are trying them or disabling them and see if that helps okay so we've got our particle system set up and now we need to do something really cool we're going to turn this into what's called a prefab we're gonna make our cloud poof particle system something that we can spawn at any time whenever one of our bad guys dies to do that we're actually gonna follow a really simple process first we're gonna rename this instead of particle system I'm gonna call this cloud particles that's just f2 to rename and then we take our cloud particles object and we're gonna drop it down into this grey area of our project view so take it and drop it in and now it's called a prefab well look here at the extension it changed to cloud particles dot prefab and our icon changed here the color change we even got this weird little button to go into prefab edit mode so what this is is a reusable object that our other objects can reference and create their own copies of so with the way that we're gonna use this is in our enemy script so let's go select our monster double-click on our enemy script and open it up now when we destroy our character we're just destroying the game object but we want to do one other thing now we want to spawn a poofing cloud particle system so to do that we need to get a reference to our poofing cloud particle system and then call a special method called instantiate so I'm going to zoom out just a little bit here and we're gonna get rid of these two extra using statements that we don't need at the top and then we'll add something in right after line four so inside of our class before our on collision enter and add two lines and then I'm gonna go with the square brace and add the serialized field attribute and remember it closes with another square brace then a space and I'm gonna say private and this is going to be a game object and we're gonna give it a name of underscore cloud particle prefab nice long name that it's really explaining what this is so we're gonna have a reference to our cloud particle prefab that's just referencing the game object or the actual prefab object there and it's going to be a serialized field so we can set this in the inspector or in the editor then what we're gonna do is use this cloud particle prefab to spawn a cloud so we'll do that right before we destroy our game object right here will say instantiate I can spell it right and then we need to give it an object of the original which really just means a game object of the original most of the time so say underscore cloud particle prefab you just type to see and let it autocomplete and I'm just gonna click on it so we've given it the cloud particle prefab we say hey we want to spawn this and we could just end it right there but this isn't gonna spawn our cloud particle prefab where we wanted it's just gonna spawn it at whatever its default position is so we need to make a little bit of a change here and we need to use what's called an overload in the instantiate so if I go back to the end of the B here and add a comma and then hit the up and down arrows you can see there are actually a bunch of different ways that I can use this method I can give it another optional parameter called the parent that's the second option notice the 2 out of 10 I can give it a parent and a option for instantiate and worldspace we don't need to know what that does right now we will really want to look at this option here number 4 where we can give it a position and a rotation and what we'll do is just give it our own position so give it transform dot position so we're saying hey spawn this cloud wherever this enemy is and then for the rotation will use a special rotation called quaternion dot identity which is essentially like a default rotation we just pass that in so this will spawn us our cloud at our position in the default rotation I'm gonna select this whole line so go to the end hold shift get home ctrl C to copy and then right before the destroy on line 25 now I'm gonna hit enter and add in one more line by pasting that in so now no matter how we die we'll spawn this cloud particle prefab now let's go back into unity and deal with the one last thing that we're missing here in fact we should see it there it is just appeared our cloud particle prefab isn't actually assigned so I'm going to select my monster and drag the cloud particle prefab right into this section but I need to do that for both of my monsters so I'm just like the other monster and we're keep that process now I'll save my scene hit play will kill a monster and see if we get a nice pretty poof Oh what look at that we actually got a poof right from the start let's stop and take a look at why that happened we have this particle system right here this cloud particle that we were playing with and working with and it's already in our scene so as soon as we spawned or started it just started playing itself and it's set to loop so we don't want that we just want to destroy it so I just clicked on it and hit delete and save and then we'll play one more time all right we'll pull the bird back launch him over crush one of these guys or it goes poof's and poof's look at that so both of our guys died and both of them played there they're poofing effect whoops and that time none of them died let's see I'm not very good at this but I think you get the idea right we're able to kill them and we get the particle two to play on them now what would happen if the particle wasn't assigned let's take a look at that real quick so let's say I go to my monster and I forgot to assign this cloud particle so I'm gonna select it the little dot here and just choose none and then let's hit play and see what happens because this happens a lot so I go back and I kill him yeah oh that was the right one that was the one that had the particle so let's kill the other one and he didn't die and look down here I've got an error message a red line there and if I go to the console tab you see it says unassigned reference exception the variable cloud particle prefab of enemy has not been assigned you probably need to assign it on the enemy script in the inspector so if I double-click on it it's actually not gonna open up the error but if I click right here it should open up the error of the line where it blew up in code let's go back and look at that one more time so here it says enemy : 12 it means that it's in the enemy script on line 12 so saying hey it blew up here because this thing wasn't assigned if we read that message we can tell kind of what it is saying that that cloud particle prefab wasn't assigned so it just means that we need to stop go find it and go to our project view and reassign it now also if we've got it already assigned here and we duplicate this guy remember it's controller command D and move him up here maybe put him up there he's gonna have the cloud particle assigned we don't have to worry about it we're not gonna have to reassign it but if we had copied one before we did that then our new version wouldn't necessarily have it all so he's not in our target group so the camera is not necessarily keeping him visible all the time so we'd have to add him to that if we wanted to keep him on there so far we've only built one level and this wouldn't be much of a game if we only had one level so now we're gonna talk about how to make multiple levels how to transition between these levels and save them all off so you can build up a game with as many levels as you want in all different designs and patterns and move guys around add in your own enemies whatever you want to do afterwards so get started we need to create another script we're gonna right click in the project view hit create choose c-sharp script again and again we're gonna type out a name with what's called Pascal casing so we'll start with an uppercase L it's gonna be a level and an uppercase C controller see if I spelled that right there we go level controller so this is gonna be something that controls what level we're on of course it opened up to Visual Studio didn't go to my file so I'm gonna go back into unity double click on it one more time and have it pull up our file now we're gonna get rid of the start method because we don't need it but we are gonna use the update so I'm gonna leave it hanging around so I'm going to delete a start and what we're gonna do here is type on enable and this is a method that gets called whenever this game object is enabled so as soon as we turn on our level controller it'll run if we turned it off and turn it on again it would run again what we want to do now is save off all of the enemies in our scene the goal here is we're gonna find all of the enemies available and once they're all dead we'll say hey we finished it's time to go on to the next level so we're gonna do that by saving our enemies into a special variable or an array of enemies which is the type of variable that we're going to use we're going to say underscore enemies space equals and now we're going to use another special method that's part of this monobehaviour let's zoom in just a little bit and it's called find objects of type and it's very important that you get the s here there you go with the plural one there's a singular one that's just find object of type and we want to use the find objects of type which is gonna find all of them if we use the singular version it's just going to return back the first enemy and it's not going to be this collection of enemies that we want so now need to give it the type which is that less than and greater than where we put our type inside it and that's gonna be the enemy and then we go to the end I just hit the end of key to get there and do open closed parenthesis and a semicolon now I'm gonna hit the HOME key to go back to enemies and I'm gonna hit alt and enter and it's gonna pop up this little option to generate a field and I'll just hit enter so what that did was it created this array variable or this array field inside my class right here on line seven of enemies this little bracket means that it's an array means that we have more than one we can use them all we could go through each one of them and look at data about them and that's what we're gonna do again the underscore is just a naming convention to say that hey this is part of my class it's not a parameter that got passed in or something that's part of just a method it's actually a field on my class and again it's a naming convention it's not a hard rule but it's something I would recommend you follow now we need to deal with how to check if they're all dead so that's gonna happen in our update so don't know if our enemies are all dead we can just say for each and this is going to loop through all of our enemies let's say for each enemy spell right enemy so it's the type enemy and the variable enemy in underscore enemies and then we'll add some braces around here so what this is doing is it's going to loop over or go through each enemy and it's gonna put them into this variable here and then run this code so we can say hey I want to do something for each of these enemies I want to run a bit of code so I can say for each enemy in enemies and I'll get this first enemy the first time through and then it'll run the second enemy through or run the third enemy through and so on so what we'll do say if enemy is not equal to no return so what we're doing here is saying hey if this enemy and our enemies list is not dead yet because once our enemy dies in this enemy script it gets destroyed and the enemy is going to be changed to no it'll no longer exist or be destroyed in our scene so if the enemy is not equal to no we still have an enemy alive we don't want to finish or go to the next level because we haven't completed it yet so we'll go through each enemy if any one of them are alive we'll just return and exit out of this update method so why do we do that well because if we don't kill all of them or I mean if we do kill all of them and none of them are not null we're gonna get to this line 23 and here let's start by just writing out a log entry and we haven't talked about log entries a lot of the time people go straight to them but I think that they're relatively boring but here I think it's worth just pointing out that we can do it so I'll say debug dot log casing matters again and then we'll do open parentheses and then we put our log message in quotes and I'll say you killed all enemies you need to make sure that you're using the correct quotes it's just the quotes right next to the enter key the one next to this little single quote don't use the single quote use the double quote and then we'll close it off with a semicolon so now if we've killed all the enemies we should just say in the log every frame hey you killed all the enemies let's go try that in fact let's um what I'm gonna do here to make this even easier on myself I'm gonna go select all of my crates I'm gonna uncheck them so now all of my enemies should just fall to the ground and I should be able to slide right into them and then let's go to the console this is where we'll see our log and make sure that you have this little button on this is the info button this is the warning button and that's the errors one I'm gonna turn them all on ok we'll pull it back we shoot and I didn't kill them all okay let's try and making this even easier I'm gonna disable all of the monsters but one fact I'm gonna delete them well yeah I'm gonna delete them right out of here I'm just gonna leave in the one monster and I'll hit play and we'll go fly over there and kill him and well you didn't see our log entry did we so why didn't we see our log entry let's talk about that for a moment because this happens all the time so we created a new script we created our level controller let's zoom this out a little bit in fact let's get rid of these extra using statements up here but we are not actually putting this in our yet we haven't put a level controller in our game we made a script for it but there's no game object for it so we need to go in here and hit game object create empty and then add a level controller component so type an le ve and it should autocomplete I'm gonna reset the transform just because I like having all of my game objects that aren't really a thing representing an object in world space to be at the default position here just so that it's really clear that this is just a special object that's running this script now I'm also gonna name my game object level controller because I want to be able to find my level controller easily I want to be able to look over here and say hey there's my level controller alright let's hit play one more time go kill this bad guy again and look at that you killed all the enemies just spamming along in there okay so we killed all the enemies that's good but what if we want to go to our actual next level well to do that first we need to make a couple little levels let's go re-enable these crates hopefully you didn't just remove all your crates and then I'm gonna go to my project view and look at our scenes subfolder so our scenes subfolder just has this sample scene I'm gonna switch to list mode just by dragging this down we just have our sample scene right now let's do this file save as and let's go to the scenes folder and let's call this level 1 with no spaces keep it nice and simple level 1 no spaces in it then let's add a couple more crates here so let's just select a new crate I'll duplicate this one I'll put it up here maybe duplicate that one and put it right there duplicate that one drag it over and drag this over get these in kind of a weird position and maybe I'll take this monster here duplicate them and put another one up top and remember it's control or command D to duplicate or you can right-click in here then I'll go to my target group and I'll make it look at my two monsters I've got one monster there got the other monster there and then I'll do file save as and we'll call this level two but we're gonna put it in that scenes folder again level two okay so now we've got two levels and we want to be able to go from level one to level two go open level one by double-clicking on it here and we can also open level two just by double-clicking so we can go back and forth I'm gonna go into level one though and we're gonna do one more little thing to make it so that our levels can actually load actually first let's see the problem so let's go into our code and what we want to do now is say hey when we finish this level you've gotten to this point load the next level so say here we want to load our next level to get the next level or to load it what we'll do say string next level name equals and then we'll put quotation marks and it's going to be level because we're naming our levels level 1 2 3 4 5 whatever and then we'll say plus and then here's where we need a special variable to keep track of the next level name or the next level ID so I'll say underscore next level index I think that's a good name for it now next level index doesn't exist yet that's why it's got this little red underline here so I'm gonna go right above my enemies right after line 4 I'll add a new line and I'm gonna say private static int which is an integer this just means it's a number between negative I forget some giant number 2.5 million or something or billion and positive that number so this is just a whole number with no decimal points and we'll call this next level index so now we've got an integer and this static keyword the static keyword means that for every level controller that we have they're all gonna reference this same variable so if we had a different level controller on every scene which we do the enemy's forward is going to be different it's gonna have its own list of enemies that it fills on its own on enable but they're all going to share this next level index so if I change the next level index on the level controller in one level it's gonna change for the next level index for every other level controller it's essentially always referencing the same number it's not its own number per controller and that's really what the static keyword means okay so we've got this next level index and we want to set it to one because we're going to increment it and then load the next level so let's set it to one because we're loading level one at first and then when we finished it we want to increment that level index get our level name and then load that level name so to increment our level index will actually add one line right here above line 24 and we'll say next level index + + so what's gonna happen here is next level index will start off at one once we've killed all of the enemies and it gets past this part of code next level index will increase by 1 + + just means add 1 to a number so it's gonna go up to 2 and then our next level name will be level 2 and it's gonna combine this into a single string or single bit of text that says level 2 the final thing we need to do is call scene Manager and remember it doesn't autocomplete because we don't have using unity engine scene management in here but if I hit alt enter so you try it again I usually can get this using unity engine that scene management option to pop up and click on it if you don't get it to pop up you have a problem with it just type it out using unity engine scene management with semicolon right here on line 2 so I say scene manager dot load scene and again open parentheses and then we'll just give it next level name we add the semicolon and save and we are almost done so go ahead play will watch the 1 error and I'll show you how to fix it and then we'll be able to load into our level so we play I'm gonna go to this console window again so I can see my errors up here pull back and let's see if I can kill this guy I might take a couple tries all right come on little blue guy there we go he's dead and look at this we've got errors it says you killed all enemies seeing level two couldn't be loaded because it has not been added to the build settings or the asset bundle has not been loaded to add a scene to the build settings use file build settings he killed all enemies level 3 couldn't be loaded you killed all enemies level 4 couldn't be loaded so let's look at this part it says go to file build set in so I'm gonna stop playing go to file and build settings and look here we have scenes in build right now we have that default samples that's not what we want though we want all of our levels in here so I can hit the ad open scenes button that's gonna add level one because it's open but I can also go to project view go to my scenes folder and drag level 2 in and now level 1 and level 2 are there so I go to file and save project and it's very important that we do this go to file save project because otherwise your build settings may not save that's the only real way to force them to save now that I've got that done we can hit play and we should be able to load into level 2 so we killed this guy look at that just like a pro and we load it into level 2 and my level 2 design isn't the best right the guys are already kind of falling and dying on their own but I hope you get the idea and now you can go through and just create as many levels as you want just make sure that you add them all to that build settings and then have your friends or family or whoever play through it and hopefully have a big blast with it one last thing that you might have noticed here by the way is that cloud poofing particle it just keeps going to turn that off and change it go to assets go select your cloud particles and uncheck that looping option right there I left that on so our cloud was just constantly replaying its particle every 5 seconds so since this is on a prefab we won't actually need to change anything in the scenes we just make that one change there alright I hope this is a really helpful video for you and helped you make your own game and I think that you should be able to build quite a bit off of this just keep adding in new enemies adding a new obstacles make some fun levels and just have a blast with it if you're really interested in this kind of stuff I'm going to be doing another video where I follow up on this and dive into how to keep things nice and clean how to really refactor your code make it really extensible and make it so that we can build out an even bigger project without things falling apart and it will talk more about performance and what we call the solid principles just clean game code so if you're interested in that make sure that you're subscribed and you hit like and all that stuff also if you're interested in more in-depth training I have a couple courses available I'll have a link below you can go check them out and see if they're something that fits for you otherwise just enjoy building games keep at it and have a lot of fun with it it's a great career and a great hobby I highly recommend it for everyone you can have a blast and you know you get to make things like this for for work and fun alright thanks again I really appreciate it don't forget to Like subscribe and share and all that stuff and goodbye
Info
Channel: Jason Weimann
Views: 2,060,019
Rating: undefined out of 5
Keywords: unity beginner tutorial, unity, unity3d, beginner, coding, programming, make a game, angry birds, how to make, game development, game development for beginners, game development unity, game, course, learn, make games, unity angry birds tutorial, unity3d game tutorial, unity3d tutorial, make a game in unity, game designer, game development course, learning unity, unity 2d, unity tutorial, unity 3d, unity2d, how to use unity, Unity beginner tutorial 2020, brackeys, jason weimann
Id: OR0e-1UBEOU
Channel Id: undefined
Length: 184min 42sec (11082 seconds)
Published: Sat Feb 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.