How to Make a Game - Unity Beginner Tutorial - 2021 Version!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there today you're going to learn how to make your own video game i'm going to teach you everything that you need to know to make a game just like what you see on the screen today even if you've never written a line of code before never thought about making a game before i'm going to take you through the process step by step make sure that you don't get lost and make sure that you're successful getting your game completed and then being able to customize it and make it your own you'll be able to make games at the end of this share them with your friends and have a lot of fun along the way it's a really cool really fun useful skill to have it's a great career to get into if you're interested in game development it's one that i've loved throughout my entire life and i don't really know anybody who's gotten into game development and hated it so if you're into game development you just want to know how to make a game you're thinking like hey i need to learn how to make my first game or maybe you're just trying to get started out and you want to see the full process of making a game that has a couple of levels and a couple different mechanics in a really clean and simple way then hey save this video hit the like button subscribe and follow along we're going to go through the entire thing also at the end of the video i'm going to do some extensions to this i'll add on some extra features and some things that people might want to see things like adding this little dot line that you'll see right across my screen to show the direction that we're going so if you have requests for features that you want to see or other things like that also drop them in the comments below and let me know and maybe they'll go into one of those extensions all right let's get started to build a video game we're going to use a game engine this is the common way that games are built and we're going to use the most common game engine used and that's unity it's my favorite game engine i've used quite a few of them in the past and even done a little work building my own but i really prefer using unity to build games that allows us to get right to the game development part and start doing fun things right away get right into the things that make our game special and unique so to set up unity we need to first download it and downloading unity is pretty easy there are a couple different ways you can go about it usually what i do is just open up a browser and just search for download unity that'll give me a couple search results and usually it takes me directly to the download page you could also go directly to the unity home page unity3d.com but i like going to the download page because right here i can click on download unity hub and set up the installer the fastest way possible so i hit download unity hub and then i'm going to allow it to download my unitysetup.exe which is what 50 megs i'll hit run or i can hit save and go find the file but i like to just hit run hit yes when it pops up asking me if it's okay and then close out my browser for now so here i get the license agreement i'll read through the entire thing really really fast and then agree to it let it go to the default folder clicking install and watch as it installs the install for the hub should be relatively quick i'm going to hit finish once it's done and let it run now the hub manages unity installations in projects so we can have multiple versions of unity installed in multiple projects and kind of bounce between them without having a bunch of folders or shortcuts on our desktop so here it's given me the firewall alert saying that hey unity hub needs firewall access i need to allow this or else we're going to have problems so i hit allow and we're good to go now in some versions of the hub there's a little pop up here that might say you need to create an account and it wants you to install unity 2019 if you get something like that or you get one from maybe it says install unity 2020 with a pop-up you can either follow that process and go through it or you can cancel out and create an account yourself either way though you're going to need to register an account so one of the first things that you'll need to do is click up here and hit sign in if you don't have an account yet you'll need to go ahead and create one to create one we can we need a username so i'll give it my name and a number there we go two and my full name perfect all right now we'll create our unity id oh i've got to agree agree and agree and then create my id then i can go into my email and i should have something that looks like this it comes really quick and just hit the link to confirm once that's done it should confirm my account there we go i gotta verify i'm not a robot i guess and then verify it and i should be good to go now if i come back over to unity i should be able to hit continue and get logged in so the first thing i'll need to do is add a unity version if you ran through the automated installer you might already have one but if not you're going to want to add a unity 2020 version so we'll hit add and i'm not going to choose the recommended release right now because right now it's recommending a unity 2019 version and we're working in 2020 to use some of the latest features so i'm going to choose an official unity 2020 release and make sure not to choose a beta or an alpha or any pre-release version there we go i've got 2020 selected i hit next and now we get to the modules section and i'm going to leave the dev tools or the microsoft visual studio community option selected if you have visual studio code that's because you're on a mac go with that i'm not going to check anything else but you're welcome to install any of the other modules that you feel you need i don't think that we need any for this part right now so i'll hit next and agree to the terms and then hit done and hit yes to allow it to install after maybe five ten minutes the installation should finish and we can continue on so i'm just gonna let it run and let it go through the entire process now how long it takes to install by the way is gonna depend on your system and your hard drives the faster your hard drives are the faster this is going to install the best system to install this on is something that's running like an nvme ssd hard drive if you have a slower hard drive it just takes longer it's not going to prevent you from working on it though and it's not going to prevent you from developing you can really do game development on just about any type of computer it can be an old laptop a brand new system a super high-end system it doesn't really matter some of the best games are built on crappy old things that are falling apart it's really about just putting forth the effort solving the problems and getting the job done the hardware helps but it's not a mandatory thing it's not really a requirement it's not something that should ever stop you from building games or moving forward so here we go it's almost done with my install i'm just going to let it keep going and stay quiet for now once the install is finished it should look something like this and it's time for us to create our first project so we have a unity 2020 installation ready we'll go to the projects tab and hit new then you'll get a new project creation wizard this is going to give you a couple different options first is we need to choose a template it'll start on 3d you'll see there's a 2d a high draft definition render pipeline a universal render pipeline and then a couple other options down here and these are changing as time goes on they're adding more and more of these starter kits or starter templates what we want to kick it off with though is the 2d template don't start with any of the others we'll just begin with 2d we also want to choose a location for our file i'm going to put mine in c colon slash projects or it's not really a file it's a bunch of files it's our set of files for our project so i'm going to put it in this projects location and i'm gonna give our project a name i'm gonna call this um i was gonna call it mad birds but i think i'm gonna call it terrible tweeters because they're just mad birds and i don't know it's two t's let's hit create and allow it to create our project so here we are unity has started up and created our new game or our new project and it's all ready to go although there's really not much to see here there's a lot of windows and a lot of boxes and weird text but if you're not familiar with unity it probably just looks like a bit of a confusing mess we have a couple different key areas i'll point them out real quick and then we're going to jump right into pulling in some art so the first is a hierarchy that shows everything that's in our current level we'll talk about that once we put some things in our level this view right here is the scene view and it's showing kind of a in editor view of what's in our level so this is a list of it and this is a visual view of what's in our level and then down below you'll see a project view which is more of a file structure or folder structure of all of the assets that are in our project this will be everything from our 2d images to 3d models our file all art files really sound files music textures what else would there be prefab files there's a lot of different stuff text files anything that's in our project is going to be right in here including our code that we'll talk about later so let's start by getting something into our project view but first let's make sure that yours looks like this if it doesn't look just like this you can always go to window and then look for layouts and choose default that'll bring you right back to this default layout view so to get started with art we're going to download what's called a unity package and a unity package is really a zip file or a compressed file that contains a bunch of other files inside of it and usually it contains a mix of art files and other unity project related files prefabs or code things or materials which are essentially art files so we're going to download one of those and import it into the project now to download it you just need to go to the link below or game dot courses slash birds that's what we're gonna do right now now on this page i can just hit the download now button to grab the package but i did notice that if i'm on edge and i hit download it actually tries to rename the file so i'm going to hit save and then i'm going to open the folder and make sure that it has the correct extension it should not be gz it should be dot unity package like it was when i saved the file so if you see it as a gz file you need to come in hit f2 rename it and put in unity package all one word we just need to change that extension and then hit yes we want to change the extension we should see that little unity logo or unity icon show up now again if you just download this in firefox or chrome or anything other than edge this doesn't seem to be an issue it just seems to be something weird that they're doing with compressed files and renaming them and accidentally catching something they shouldn't so now i've got my bird art file here i'm going to close out my browser and i'm going to leave my unity window up i've got my downloads window here with my bird art up and my unity window there i have two options well really three options i could first just double click on this and if this is the only unity instance that i have it's going to import this art into the project the other thing i could do is just click and drag it into the project view as well that way i can define which project it's going into if i happen to have multiple unity projects open you should only have one open right now though but just in case i want to make sure that you understand that process so when i do that i get this import unity package window that pops up and it shows me all of the things that are going to be in this package it doesn't show me a really good preview of them but i can get a list of the file names i can see i've got an art folder with some birds some environment stuff some monsters some props and some ui then i have the option to choose none unchecking them all go check individual ones or just leave all checked which is what we want to do and hit the import button that's going to pull in all of the files underneath this art folder just like the preview showed i'm going to click on it and then i'll expand it out with the little arrow here and then click through some of these folders and take a look you see that we've got some birds here we've got two different birds in four different states each we've got an environment with a background and a piece for the ground you'll see how we use that later to tile we've got a monster here with his eyes open and closed or his eye open and closed we've got some crates for props and a little dot for our ui so now we're ready to start building our game and start creating our first character the first character in our game is going to be our red bird so let's go into the birds folder and then find the red bird with his eyes open so here i've got red beak or beak red open one and i think that's the one i want to use if your view doesn't look like this by the way there's a little slider here that allows us to make these images bigger or shrink it down all the way to a list view nice and small i'm going to go i think with the biggest size for now and just start with this beak red open one and this is going to be my first character or my first bird i'm going to take him and i'm going to click and just drag him out into the scene view you can see that as long as i hold the mouse button down i can move him around if i let go anywhere he's going to just drop in there and i'm going to get this red beak open one placed in a position now if you got this in a position that you can't find you can't control it anymore you can just click on it hit delete and try again so now i'm going to take it and do it a little bit different this time i'm going to take my red beak open one and i'm going to drop it into the hierarchy the one difference here is that that's going to put it right in the center of my screen and if we look over here at the inspector you'll see that it's got a position value and it's going to be at 0 0 0. when i dragged and dropped one out into the scene randomly like this watch that value is not zero zero zero it's somewhere else random in the world or not really random but at the position where i dropped it because here i dropped it below the middle point it has a negative y value and because i dropped it to the left of the center point it's going to have a negative x value watch as i drag this right to the left by the way grab this x value and just drag and watch as the bird goes to the right and to the left and i can do the same with the y value here i'm going to delete this second bird and just work with the first one the first bird is a little bit hard to see because he's behind the camera and there's a little camera gizmo that's showing in front of him if i click this gizmos button it'll go away but i don't really want to do that i want to keep my gizmos visible because that's showing me my camera outline what the player can see that white box and it's showing me other things that don't necessarily have an in-game visualization like a camera in a game doesn't necessarily have an object there that shows as a camera in fact it usually doesn't so i want to first just move this red beak open guy a little bit to the left and to do that i'm going to move over to my move tool right here right below this edit button is a move tool and that's also w on the keyboard as a hotkey to get to once you're in the move tool you can grab the arrows here either the green or red arrow and just drag it left or right i'm just going to drag it over to the left a little bit and then maybe grab the blue box and drag him up here so he's kind of up and into the corner now i'm gonna save my level saving off my scene is pretty simple to do that i just hit file save or hit control s on the keyboard nice little shortcut and then i'll hit play and watch our game in action so hit play and look at that we've got a bird that sits there and does absolutely nothing it's not very exciting for a game so we need to start adding in some functionality or some movement or physics and that's what we're going to do next so to make our bird work with physics we need to add in some physics components and first we just need to make sure that we have our red bird selected and right now it's named beak red open one i'm gonna rename this to do that i'm gonna click on him and then go over to the inspector tab where we'll be working and just rename him to red bird i gotta spell it right red bird there we go so that it's a little bit easier to say and see and name now i want to look at the components that we have we have first the transform component that's the one that i just showed you where we could drag and move it around and it updates as we move the position around and also use it to scale the thing up and maybe stretch it out or whatever we want to do rotate it but right now we're going to set that back to 1 leave the rotation alone and leave the position right around there we're going to skip over the sprite renderer because we're going to talk about those a lot later and we're going to go down to the add component button here we're going to click the button and then go under the physics 2d section there is a physics section that's for 3d stuff and it does not interact with the 2d physics system so make sure that you go under physics 2d instead so go to physics 2d and what we want to look for is the rigidbody 2d option i'm going to check that and then i'm going to hit the play button and watch what happens there we go our bird is now falling our bird is actually implementing physics now and using physics in a real world type of way it's not perfectly real world but it's pretty close what's happening is he's sitting there and there's nothing underneath him so he just falls just like any object would if we have gravity there now it's worth noting that we have a gravity scale value here so if i set this to zero and hit play he's not gonna fall at all watch he'll just sit still and i can even adjust this at run time i could set this down up to one maybe or set it to zero and set it to negative one and watch him start falling upward there we go now he falls upward so he's gonna fall in the direction of the gravity at whatever multiplier we set it at the default now is one that just makes him fall down at a semi-realistic value or a realistic speed so now he's falling but that's not much of a game right we want a game where we can launch a bird off and make him fly into things so we need to do a couple of things we need to first set it up so that we can control the bird and make it so that we can control which way he's flying and falling i said first it's really last first though we want to make it so he can collide with things and make it so that when he falls and hits things he stops maybe he knocks those things over so that when we launch him off he's not just flying through things and flying through space now to make him collide we just need to pull in some ground or something for him to actually hit so we'll go to the environment folder we're going to take the summer ground piece and drop that into our scene i'm gonna put it somewhere below the bird so that he could fall down onto it and if i hit play and watch what happens we should see that well our bird just kind of falls right past it and that's because while we've told our bird to fall and deal with collisions we haven't yet told our ground that it needs to deal with collisions and we haven't completely finished setting up our bird for collisions as well so we need to do two things first is to select the ground and we need to give it a collider we need to tell the physics system hey physics system this object should be collidable because remember not everything in a game should collide our background shouldn't collide that would be terrible right if you collided with the background you'd just be walking on the top of it or something so to do that we're going to add what's called a collider and since we have right here a pretty square shape or a box we're going to add what's called a box collider by the way to zoom in and out on the scene view just use the mouse wheel and use the right mouse button to drag around and zoom or kind of pan around as you zoom in and out so i'm going to add a component and i'm going to go to physics 2d and i'm going to look for box collider 2d notice that that puts a little green box right around our box here i'm gonna hit play and watch what happens with that we still fall right through so the other part that's missing was on our bird our bird also needs a collider if you're thinking hey let's add a box collider to that well we could but is our bird a box shape i'm going to say not really we could also maybe use something like a circle collider but there's actually a perfect collider for polygons or sprites like this called a polygon collider 2d so that's what we're going to use we'll hit add component we'll go down to physics 2d and then search for the polygon collider 2d option right here we'll add that on and notice that we get a green outline that matches with our sprite the way that this works is that it uses the alpha or transparency of the sprite to kind of build this mask and we can modify it we can actually click on it and click on the edit button here and drag in and modify these points around if we need to i think that the default one for this is probably perfectly fine though but if you ever need to go in and modify one for your own colliders that's how you just hit this little button and go in and redraw the collider around it so now we've got a collider on our bird and one on our summer ground i'm going to hold ctrl and select them both so you can see the green around both of them and then i'm gonna hit play and watch see what happens there we go we now have collisions our bird can hit things now before i go launching this bird i think that we need a little bit more area to hit or area to land so we're going to take this ground piece and stretch it out we're going to make it wide really really wide so that we can use it as the ground for our bird no matter how far we shoot him he can hit that ground piece so i'm going to zoom out here with my mouse wheel and then i'm going to rescale this thing so if i just grab the scale value here and start stretching it out you see that it looks pretty terrible it stretches out and distorts not the effect that we want to have so i'm going to set this back to a 1 and we're going to change and do something else instead what we're going to use is the tiled mode on our sprites right now if you look at our sprite renderer the draw mode is set too simple i'm going to change this over to tiled which allows us to do this stretched out or repeated tiles of a sprite so now that we have it in tiled mode i'm going to hit the t key or click over here to the rect tool you'll want to do this when we start scaling this so click over to the rex tool or hit t and then grab the right hand side and just start dragging and look at that it starts stretching out further and further and i can drag out way past the screen i'm going to zoom out here and just drag over to the left as well and then zoom back in a bit now notice that our collider is a little bit weird we've got this green box here and it's just in the center and if i hit play it's not going to work anymore watch our bird he falls right through we need to actually set up our collider to use tiling as well luckily that's as simple as checking this auto tiling box now that green went all the way to the end or both ends and if i hit play i now have a nice long ground piece that i can land on i'm going to stop playing and move this down a little bit and then i think i'll call it good with my ground so i've got my ground i'll hit w to switch over to the move tool or click on the move tool and then i'm just going to move it down a bit but i think to move it first i want to move over to grid snapping mode so let's do that now grid snapping mode is a mode that allows me to move things so that they don't move along with decimal points notice as i drag this up and down i'm getting very fine movement down to like the 10th or 100th of a point or a hundredth of a unit of movement and in a game like this i really want to move on maybe like one unit or a half unit marks so i'm going to switch over to grid mode to do that i need to check this box but i can't do that until i switch over from local to global positions or global rotation is really what it's about so i click on the that button and this goes to global now i can check on the grid snapping and it'll turn it on it'll get this bright white light up look and turned off it'll look like that so i'm going to turn it on now watch as i drag this around look at the value it's going four five six three nice and even numbers and i'm going to drag it right up here so the bottom of it is lined up with the bottom of my camera and then i'm gonna just drag it over so that the x position is at zero so that it's just nice and centered and it goes on for a really long ways to the right and to the left now we're going to pretty things up with a background so we're going to take our summer background and we're going to drop that into the scene as well i'm going to just grab it and click and drag let's see if i can do that click and drag and there we go i've got a background kind of plopped right down in my scene i'm going to click on it though and just move it so that the position is lined up so that it's just above the background or the ground piece there so it kind of lines up and tiles neatly now i want to stretch this background out as well so i'm going to switch the draw mode over to tiled and notice here that it says sprite tiling may not appear correctly because the sprite is not generated with full rect or sprite modis set to polygon mode this is an issue that you didn't see in the summer ground because i already switched the mode for you but i want to show you this on the summer background because when we set up tiling on a sprite we generally need to do this we need to go to the sprite click on it to find the source one and change the mesh type from tight to full rect and then hit the apply button once i do that and i go back to this background and i have tiled mode on notice that that error message or warning has gone away now i'm going to zoom out hit the t key again to go back to that rect tool and then just start dragging this out stretching out the background to kind of match with the ground piece that i have there so now i've got a nice long tiled out background and i'm going to set that position to zero again just so i can center out the starting point if i go to my game view notice that i'm in the scene view tab we haven't looked at the game view i can actually click through to the game view without hitting play and get an idea of what it looks like i'm going to hit play one more time just watch my bird fall onto the ground in this background and then hit play again and get back to work now depending on your system you might be seeing something a little bit strange you might notice that your bird isn't actually showing up so you might see that your bird is just kind of invisible here i've actually switched over to a completely different computer to show exactly how this happens what's really going on is that the order that these things are drawn in isn't being controlled it's somewhat random and it's left up to chance right now we're not directly controlling what things should draw on top of what other object so when we put them in the first time it seemed like everything was fine but if i build out to another device or another platform or even switch to another computer like i've done here you see that sometimes the order doesn't line up and then my objects look like they're not there they look like they've disappeared if i go select that bird again you can see he's there and he kind of shows up but he's missing so how can i fix him let's go through the process what i need to do is really just pretty simple i need to modify the order that he's drawn in and i can do that on my sprite renderer there are a couple ways that i can do it the first is i can modify this order and layer if i just grab this and drag it to the right so the value is higher than zero you see that the bird shows up if i go to my game view now he's there again the way that this works is that everything in a shared sorting layer and right now everything is in this default sorting layer will get rendered in the order that it is set in this order and layer it'll start with the lowest value first and then go all the way up to the highest so it's going to start with my zeros and then go to ones and twos and threes all the way up to eight it also takes negative values so i could just set my background to like negative 100 and it would be way behind there's another way to do this though and i want to show you that so i'm going to set this back to zero and i'm going to go select my background so select this summer background object and i know that i want this to always be behind everything i don't want to have to change it constantly and i could change the order and layer to just drag that down to a negative value and it would work but the other option is to add in a new sorting layer i can just hit this sorting layer option and instead of choosing default hit add sorting layer and then i need to go into my sorting layers it'll just bring up this actual layers and tags inspector that allows me to do some other things for now though we're just going to worry about sorting layers you can hit the plus button to create a new sorting layer and then call it background i'm going to give it a capital b just to stay consistent now the important thing here is to make sure that these are in the correct order so i want to make sure that my background is above my default so the background draws and then default draws you might notice that nothing's changed that's because if i go select my background notice that the sorting layer isn't actually changed when we go into that tags and layers inspector it doesn't actually select the new value that we've created because i could have created 10 values and there it doesn't know what to pick so i need to go in and pick background there we go now it's showing up now watch as i go back to that tags and layers thing one more time i'm gonna hit add sorting layer and if i rearrange these look at that now my background is in front so it shows up in front of the bird and click it this way and now my background is behind also notice that that fixed up our ground so now we get our nice green edge over the ground or the nice cool ground look that we're expecting to see now that our bird is showing up right again i want to make him animate i want to make him flap around so that when we drag him and make him go launching he doesn't look like just a static bird sitting there floating around on the screen so to do that i'm going to go into my art folder and go to the birds subfolder and i want to take my two open beak birds i'm going to switch over to list view mode though remember this little slider here can drag it all the way to the left and go into a list view and i'm going to take the red open 1 and red open 2 and hold shift and select them both so click the first one hold shift click the second one and then i'm going to drag them into the hierarchy this should pop up a create new animation dialog that's going to allow me to create a new dot anim file that wants me to give it a file name i'm going to call this red bird eyes open so red bird eyes open and it's good to give your things a useful meaningful name that actually makes sense it's a lot easier than rbeo or something like that where i'm gonna have to try to remember and decipher it name things well it'll save you a lot of time in the future so red bird eyes open and i'm gonna save this in my art birds folder and hit the save button now if i hit play let's see what happens we see that my other bird falls and this guy just flaps his wings so how do i get this bird to do what this bird's doing this is my one that's going to be my character and i want him to play this animation so i'm gonna stop playing and i'm gonna take a look at these two birds that we have here when we dropped it out there and created the animation it also created a sprite out here or a game object with a sprite and with an animator component on it our other bird has a sprite and a rigid body does not have the animator component so what we need to do is get that animator component or something similar onto our bird so to do that i'm going to go collapse these two components here and then go to my beak red open i'll right click on the component and hit copy component go over to my bird and then i can click on this little dot here and there's a paste option paste component as new let's drag this over to the side so you can see it i can hit paste component as new and that'll drop it on there so now i have this animator on this character let's see what that looks like if i hit play there we go now he falls and he flaps his wings looking much better i'm gonna delete the other red bird open one so select this guy hit delete on my keyboard and get rid of him now i've got a single bird that animates and is ready for me to launch him or write some code that can launch him all right now we're going to write some code this is the fun part if you've been scared or worried about this don't be it's going to be a blast and it's not going to be too complicated or anything too hard to understand you'll see that the code that we write is relatively simple to get the job done we don't have to do anything really complex or confusing and you don't have to understand a lot of math or anything else we're just going to write some simple code to handle input and make a bird go flying so to get started we're going to need a code editor now i'm on windows and the default editor in windows is visual studio and visual studio community let me show you where that set before i open it up if you go into edit and then go to preferences drag your preferences window over you see an external tools section now this changes every version a little bit but it should look something like this over here you'll see an external script editor i'm using visual studio community writer is another great option by jetbrains i use that all the time you can also use a different visual studio editor or visual studio code if you run into problems and you're not seeing things like auto completion or your project isn't working right make sure that you come into this section and hit the regenerate project files that will usually fix the issue if that doesn't fix it try closing unity reopening it regenerating those files or even rebooting sometimes we run into weird issues things get out of sync and you just gotta do a quick reboot but normally if you have a problem where you can't open the code editor this regenerate button is enough to fix it okay enough about that let's talk about actually writing code so to write some code we're first going to need to create a script and to create a script i want to put a folder in here first so that i have a place for it so i'm going to go to my assets folder here we go just left click there and right click on our assets folder in this gray area that's going to bring up this creation dialog or it's really a dialogue with a lot of options but the one i care about is create up the top and i'm going to hit folder we're going to create a new folder and we're going to call this scripts scripts with a capital s it's a very common folder name for unity code files expect to see it in just about any unity project that you see some people may name it something different but the default is just going to be scripts i'm going to hit enter and go into that folder now i've got this empty scripts folder i'll right click again hit create and we're going to choose c sharp script that's the language that we're going to write all of our code in c sharp unity used to support some other languages it no longer does and you don't need to worry about anything other than c sharp so we're going to choose c sharp script and we're going to name this script bird i haven't hit enter yet or anything else so i'm just going to type in a capital b ird make sure that you capitalize the b and get the casing right otherwise you're going to notice some errors and issues or you might later because the language is case sensitive and it's going to create a file and a class name that matches so i'm going to hit enter if you named it something else or you left in a lowercase b just hit delete clear that thing out and then go in and recreate it again so just right click again create c sharp script capital b ird and then enter now i have my bird script and here you can see a little preview of the code you can't actually edit that code though and that's definitely not how you would want to so we're going to open up our code editor now we can do that usually by just double clicking on the file here we go visual studio has launched and it's way too big so i'm going to shrink it down now if your visual studio doesn't launch like this you don't get it open or doesn't happen again you can regenerate project files or watch something to close this real quick you can also go into game object or note assets sorry and then open c sharp project that should also force open the visual studio project it might not take you directly to the file though so if it doesn't take you to your bird files go back into unity and double click on it again or you can go to the solution explorer window which might be over here to the left depending on how your visual studio is set up and then you can expand it out go to assets scripts and find your bird there all right now that i have my bird script open i want to talk about what all this is because if you have never seen c sharp before this probably just looks like a bunch of random words it looks like i've got an update i'm going to ignore that for now so let's zoom this in and take a look at what this code is what it's saying and then make some modifications to it so i'm going to hit my zoom a little bit just control on the mouse wheel to zoom in on visual studio and up at the top i have two grade outlines these are grayed out because they're actually lines of code that aren't used right now so i can just ignore them this next line here is what's called a using statement and it really just says hey that this code or this script is going to use some stuff from unity's engine we're going to use some unity specific code or objects and we needed references to that so saying hey when you build this thing or when you do your compilation just know that i need unity for this now that thing that it needs is this mono behavior part but we're going to talk about that later first what you want to notice is that we have a public class named bird and that matches up with our file name here this bird and the casing matches as well if that doesn't match if i misname this or i rename this or i you know put in the number four there i'm gonna get errors my game won't work it won't even start i need to make sure that they match and that they match on casing underneath that we have a void start and this little green section which is a comment this two slash part means hey this is a comment it's not actual code it's just a note for the person that's reading the code not a note for the computer of anything to run so here's just a note saying that start is called before the first frame update talk about what that means later it just means really that it's called once at the beginning before update now again we'll dive into details later the update down here is a little bit different this is called once per frame so it means that every frame if we run you know 100 frames a second this will get called 100 times a second what that means is that any code that we write that we want to run every frame things like handling input or watching to see if we like shot at something or if we got too close to something or maybe it got close enough for a collision those types of things we would do in an update the collision would be a little bit different but we'll talk about that again too as we get a little further on so let's start by writing some code here the first bit of code that we want to write is going to deal with stopping our character from falling i want to make it so that when we start off our game our bird doesn't fall down so that it sits still and then we can drag it around and then launch it just like an angry birds type game so what we're going to do is add in a little bit of code in the start method so to do that we want to go down to line 10 right here which is in between these braces these braces define everything that is run during our start method so when this object's life starts up or when our bird starts the code inside these braces will run now the code that we want to write is going to access the rigidbody component and tell it to become kinematic let's go look at what that looks like in the editor before we write the code for it so go to unity and i'll select my red bird and if i expand out my rigid body 2d remember these arrows allow me to collapse and expand things i'm going to collapse this info i think it's a little bit useless and if i look at my simulated option what i can do is uncheck this and hit play watch what happens to the character he doesn't fall anymore if i check it and uncheck it you can see that he star starts falling and then stops falling so what i want to do is make it so that he doesn't fall by default that he's essentially not simulated in a 3d context we would be toggling the kinematic state here we're going to toggle the simulated state of this rigid body to do that we'll go right back to this line 10 and we're going to write in the word get component and we're going to add in the less than sign that's shift and comma or the key right next to m and we're going to put in rigidbody 2d now i'm getting good auto completion here in visual studio and you should as well if you're not seeing that again go back into the editor and go to the project settings part edit and go to or sorry preferences and go to external tools and regenerate those project files you should start to see it show up if that doesn't work then make sure that your unity is updated and that your visual studio is updated too sometimes they get out of sync all right so now i've got my method git component and i've got this less than rigidbody 2d greater than what that really means is get the rigidbody 2d component this greater than less than around it is what we call a generic type and it's saying get a component of this type now i need a open and close parentheses so i do shift nine shift zero and that says hey actually execute this so after every method call or everything that we want to run we do an open and closed parenthesis and sometimes we put parameters in there so if this was going to take a parameter i would put it there git component does not though so i get this component and then i put a dot because that's going to get me a reference to it and then i can do something to it now if i scroll down here i should find something for is kinematic or simulated now we want to choose the kinematic option and not simulated primarily because we want to be able to interact with this object through our player when it's still not moving around or when it's not falling so we're going to choose is kinematic and we're going to set that to true now is kinematic just means that hey it's still simulated but it's only controlled or moved by us or by our code and it's not moved or controlled by physics objects or the physics system on its own so we'll save that off and we'll go into unity and then let's hit play and watch our bird he should no longer fall but stay simulated look at that now i want to make it so that our player can interact with the bird we're going to set it up so that we can tell when our player has started clicking on them and get them ready to start clicking and dragging i think we'll start by just tinting him a different color and then maybe make it so his eyeballs close so to start with a different color we're going to do something on the sprite renderer right now i'm in play mode so my changes won't be saved watch as i change the color of this guy in play mode so here i've clicked on the color and i'm going to go over to maybe a yellow and then just drag over here and look at that guy he just turned yellow if i go around the color wheel you see it goes green blue-ish kind of a pink and a red i'm just going to go with like a red for now and what i want to do is make it so that when i'm clicking and holding down on him he turns red and when i let go he turns back to his normal white color i'm going to stop playing just so you can see that the color got reset if i'd done that in edit mode though and gone like that it would stay stuck in red and i'd have to go back over to white and just change the color back okay now let's go write the code to make this happen at runtime when our player clicks on the character so to do that we'll open up our bird script and we're going to add in a new method we're going to add in an on mouse down method and this is a built-in part of unity and part of this mono behavior class so to do that we're going to go right here to line 12 and add in an extra line i'll add in two lines and then i'll go up to line thirteen i wanna have a little bit of space between my methods i'm gonna type in the word void which means no return type or don't give us back anything we'll talk about that later and then we're gonna add in the words on mouse down and it should auto complete for me but if it doesn't you can just type it all out with an open close parenthesis and then add in the open and close braces it automatically added them for me and it automatically added this private keyword i'm going to remove the private keyword because i don't necessarily need it it's private by default and that just means that it can't be called from another class we don't have any other classes it doesn't really matter but i'm going to get rid of it to line everything up and make it nice and short again it's just telling us that it's the default i don't necessarily have to have that there doesn't make any technical difference so on mouse down what do we want to do we're going to change that color of the sprite renderer so just like we got the rigid body 2d component we're going to get the sprite renderer component so say git component of type i'm going to hit down arrow and enter and then hit the less than key because the down arrow one didn't do anything different for me and i'll put in sprite renderer with a capital s and a capital r open close parentheses after the greater than sign and then a dot at the end now this is where i get that pop-up to allow me to see the different properties on there look at there's a flip y a flip x you might have seen those on the sprite renderer and if i keep going up you'll see the color i'm going to set this color to something so to do that i need an equals which means we're going to assign it and i want to assign it to a built-in color now there are two different ways that i could do this i could just say it's equal to a new and then put in the word color and then open parentheses and then i can give it a value of like one for red zero for green one or one for blue and let it create that color so that it's a hundred percent red and blue and zero green what's that going to come out to like a purple or i can use this other shorthand which is color dot and there are a bunch of built-in ones so i could use white green red blue black clear i'm just going to go with red and make it really obvious so i can say color.red which is just a built-in one i'll save that off and i'm gonna go back into unity and try it out remember it's ctrl s to save by the way get used to that hotkey you're gonna use it all the time so hit play in here and i'm gonna click on my bird and look at that he turns red and then i'll let go and he stays red because we haven't written any code to turn him back white so let's go back into our code and add in the code to make him white we'll open up our bird double clicking on that script and we'll add in an on mouse up to do that i'll say on mouse up notice i didn't even type in the word void i just started typing and let it auto complete i don't even have to worry about the casing if my code editor is working right and then i can hit enter and let it generate if it doesn't generate for you you may have to just go in and type it all out but i'd recommend getting your auto completion working if you can i'm going to delete this private keyword just double click on it and then hit delete twice and then we'll add in a on mouse up method or handling of the on mouse up so i want to do the same thing that we're doing here except turn it to white and i don't want to type because hey typing takes time and i can just copy this and make it faster so i'm going to click here drag to the end hit ctrl c to copy and i'll click here on line 20 and hit ctrl v to paste nice and short save myself some time and now all i have to do is change this word red to white so now on mouse down we'll change it to red on mouse up we'll change it to white let's go try that out go into unity we'll hit play click he's red and let go and he's white so there we go he's turning red when he's charging up and white when we're going to launch him we're getting there we're almost to the point where we can start launching this bird off next we need to make him actually move around when we drag our mouse so to do that we'll stop playing again go back to our bird and add in another method this is going to be an on mouse drag just like on mouse up and on mouse down there's an on mouse drag so hit enter two more times and type on mouse drag and i'll get rid of that private keyword again just double click and hit delete twice and then here what we want to do is move our bird to wherever our player's mouse is to do that we're actually going to modify the transform's position let's go take a look at that real quick in unity remember we have this transform right at the top of all of our objects if i click on any of these they all have this transform it has our position rotation and scale the three kind of key things that any object in 3d space is going to need so what we want to do is move the position of this in code to match wherever our mouse is to do that we're going to use our camera here this main camera that has this main camera tag on it if your camera does not have this main camera tag this code is not going to work so you need to make sure that you have your camera tagged as main camera that's the default it would only be different if you've changed it all right let's go into the code and write the code that we need so on mouse drag we want to figure out where the player's mouse cursor is like where that position is and then we just want to move the bird to that position so to find the position we need to set up a vector or a variable to store this in so we'll say vector 3 which is a 3d positional vector it's an x a y and a z we can use it for position or other things in this case it's used for position and we're going to call this mouse position with a lowercase m and a capital k capital p because that's called pascal case or sorry camelcase that's camelcase and that's what we generally use for variables in c sharp if it's a variable like this something that we're using inside of a method we use this camel case where we start with a lowercase and then use an uppercase letter for every word after that so we have a mouse position we're going to assign it to camera.main which is going to find that main camera in our scene it actually does a search by tag and it looks for all objects tagged with the main camera and finds the first one that's a camera tagged with that and we want to use the screen point to world point method so we're going to select screen point to world point and then do an open parenthesis and what we need to give at first is a screen point for our mouse so to do that we go input dot mouse position and then add a semicolon at the end so this is actually just going to give us the mouse position in world space in our game just off of our main camera again this only works if our camera is tagged as the main camera so it knows what camera to use it's one of these nice built-in methods to find positions and map things so you don't have to do all of this math yourself so now we've got our mouse position we just say transform.position equals mouse position save that off make sure we've got a semicolon at the end of each of those and then we'll go back to unity and hit play there we go i click and i drag it around and look at that my bird totally disappeared let's see why that happened i'm gonna go select my bird and look at him well let's stop playing go select the bird and then hit play and let's look at him in the inspector so right now his position is at almost negative six 1.8 and zero i'm going to click on him and like that the negative six and one values look good but look at that z the z actually changed to a negative 10. i'm going to click on my camera notice that that's also at a negative 10. let's stop playing and think about what's happening here we're doing this shot into the game or into our scene from our camera using this screen point to world point or screen to world point and it's giving us a point for where we're clicking in here but it's using the camera's z position because our game is completely flat so we need to set the z position back to zero or leave the z position alone so we'll go back into our bird code just double click on my bird script here and when we set the position instead of setting it to this mouse position let's set it to a new vector 3 and we'll use the mouse positions x value so i do mouse position dot x as the first parameter and i'll put a comma here and this allows me to do a second parameter of a vector3 so here i'm just creating a vector that's got an x and a y value that match our mouse position and then we're going to leave the z value alone so say mouseposition.x for the first and mouseposition.y for the second parameter and then for the third parameter we'll just use transform.position.z so that way we're using our own z position we never move it and we only move along the x and y values i'm going to save that again go back to unity one more time hit play and try dragging my bird around look at that he drags around and now goes wherever i want and if i let him go i can just kind of place him and move him on the screen so i mean might have a game now oh look i dragged him behind the ground so that might be something i want to address but right now that's a ground piece that he'll hit so i don't really need to worry about i'm going to stop playing let him reset and then we'll get into adding some more stuff to him now we're gonna make our bird actually do some flying we're gonna make it launch off like an angry bird in angry birds and go flying well in the opposite direction of the way that we pulled it so if i pull it straight back it'll fly straight forward if i pull it straight down it'll fly straight upward and so on so we're going to need to write a little bit more code to do that and to do that we need to open up our bird script so we'll double click on the bird script here get it open and what we want to do well there are really two things first we want to keep track of our starting position the spot that we're in well when our game starts just our initial position and then we want to when we release the mouse or in our mouse up launch our bird off towards that starting position so we'll figure out what that start position is save it off in what we call a variable or a field and then on mouse up we'll just launch our bird towards that position so to do that i'm going to first zoom out my bird script i'm going to hold ctrl and use the mouse wheel and zoom out so you can see the entire thing and then i'm going to go right up to my start method right at the beginning of my start method i'm going to add in a new line i'll just hit enter here on line 9 to add in another space and i'm going to zoom in a little bit before i start typing what i want to do here is save off my starting position and i'm going to save that in what we call a variable in this case it's going to be a variable that's called a field because it's stored at the class level you're going to see that i'm not going to actually start typing it at the class level i'm going to start typing it in the start method and then i'm going to let the editor do some magic and just kind of auto complete for me or save me time so that i don't have to type as much now the name of this variable is just going to be start position i'm going to name it start position because this is going to be my starting position and i'm going to prefix it within underscore and that's just a naming convention or a habit of mine where if i have a variable that's not going to be specific to a method where it's going to be up here at the class level i like to do it with an underscore first so it's going to be underscore start position with a lowercase s and a capital p and i'm going to start by just assigning it to a value and what i want to assign it to is our current position and there are two ways that i could get it i could either use the transform position so i could say equals transform.position but i don't want to do that because that's a vector3 and right now we're working in 2d vectors and i want to cut out that third part of it the z part so what i'm going to do instead is get the rigidbody 2ds position so i'll say git component rigidbody2d and then i need my open and close parentheses and then dot position this will return back a vector 2. if i put my mouse over it you can see right there in the little tool tip it says vector 2 rigid body 2d position that means that that's the return type and that means that my start position will be a vector 2. now right now i've got an error saying well what does the error message say the name start position does not exist in the current context that's because so far i haven't defined my variable i haven't said hey what start position is or where it is or what the scope of it is or anything like that and you might not know what any of that means what it really means is that i need to click on this little button here and hit generate field and it's going to put in some code right here at the beginning of my class it's going to say private vector 2 start position this is how we define a field in our code so it's a variable that is saved off for our bird and it's saved once for every single bird if i put multiple birds in here each one of them will save off its own start position in its own start position variable now it does start with this private keyword here and just like with the methods down below where i said we don't need it we don't need it for our private fields either so i'm going to double click on it and hit delete twice all right now we have our start position saved off or cached i want to scroll down and get to our mouse up part and do the launching make our bird go launching off in the direction towards our start position so to do that i first need to get the direction to our start position and we can do that with a little bit of very simple vector math we'll need to get our current position and then just do a little bit of subtraction so we'll say var currentposition this is going to be a variable named currentposition equals component and we'll get the rigid body 2d oh i got to type in the 2d part right there with the open and close parenthesis dot position so this is going to save off our current position which is a vector 2. i could also type this as vector 2 current position it's just that i know that it's a vector 2 so i didn't it doesn't matter either way var is just a shortcut for different variables and it'll let the editor just say hey you i don't want to type out the name of it just know that it's a variable and understand what it's going to be by what i'm assigning to it so since i'm assigning a vector 2 to it it would be a vector 2. but we can just use vector 2 as well i just want to show you that var syntax because sometimes you're going to see it and it might be a little bit confusing just know that it's a shorthand for just any old variable it doesn't mean that it is any old variable it just means that we're not writing out the name of it so we've got current position there now i said we need to get the direction to get the direction we just need to do some subtraction so say vector 2 direction equals and then we'll use our start position so underscore start position minus our current position now what this is going to do is subtract our two vectors it's going to subtract that start position from our current put it into a direction and a vector 2 is just a vector or a variable that has an x and a y component so it's really just a x value and a y value that are floating point numbers just numbers with decimals that could be positive or negative essentially what we're seeing right here let's go to the bird and just look at a vector here's a vector three where we have x y and z when we access the vector or position from the rigid body 2d component since it's 2d it has these two just not that z value now let's look at why we would do that subtraction of the start position minus our current position let's say that our start position is right here in fact let's move our start position right now to zero and zero because i think that that makes it a little bit easier to understand the math here so our start position's at zero zero and imagine we've drug our bird to the left one meter so we go to negative one he's now gone one to the left in fact here let's move him even further to the left move him negative two to the left imagine that's exactly negative two there we go now it is exactly negative two so what would the direction be if i did some subtraction here if i subtract my start position and then my current position so my start position would be 0 minus negative 2 which means that my vector or direction is going to be a2 so that means that if i move my bird with a vector with a value of 2 on the x so say that i moved him in that direction just one time i would do a plus 2 here oops let's see if i can type that again plus can i get my plus in there plus two and he would move right over there obviously i don't want him to move that fast every frame but that is the correct direction that's the way that he would go if i moved him to the left and down and did the subtraction again you'd see i'd get a positive 1.3 and a positive what is it 1.27 here for my direction that would be my vector the x and y values of it so that's going to give us the direction and the distance though not just the direction because it's 1.3 and if i drag it off you know 5 meters to the left it would be 5 instead of a 1.3 or whatever so we need to add another line to the code that will remove the magnitude or the distance from our vector and make it just what we call a normalized vector where the total magnitude of it becomes one that means if it was only going horizontal the magnitude would either be negative one or positive one if it was only going vertical yeah then it would be positive or negative one on the y and if it was going like at a 45 degree angle you're going to get what is it 0.707 on both of them and you'll see how those values line up later i think but for now just know that we need to use the normalize method on it let's go into the bird script and add that in so we'll go right below our line 24 where we're assigning our direction we'll say direction dot normalize right here not normalized by the way normalized returns back the normalized value of it normalize we'll just change our existing vector very different the way that they work although they both give you the same thing so now we've got a normalized vector we've got our color getting set back and uh what do we need to do well we need to launch our bird off in the direction of our well normalized vector or in this direction so to do that we're going to go in here add in two more lines because i want a little bit of spacing and i'll say get component again and we'll get the rigid body 2d one more time and here we're going to add force in that direction so we'll say add force and then we give it a vector 2 which is a force amount or a force vector we'll just give it direction and then i want to multiply this i want to give this some value that i can set on the bird where i can make this go faster or slower we're going to start with just multiplying it by a number like five though so i'm going to put star which is our multiply and then five so this is going to add force in our direction times five so whatever direction it is we'll launch it off at a five we'll see if that does anything or if that value is way way way too low let's add a semicolon at the end save our code off and then go into unity hit play and see what happens all right so i've got my bird still at zero zero zero i'm going to leave him there for just a moment we'll click drag and release him and well nothing really happened at all he just kind of sat there so let's go take another look at the code why didn't he go flying well if you remember just a little while ago let's zoom out we did set him to kinematic and he is not going to go flying on his own so in our code where we launch him we also need to set him to not kinematic so i'm going to take this line here on 13 and select the whole thing just click and drag to select it and hit ctrl c to copy it and then right above my add force line here i'm going to hit enter one more time and paste and then i'll change this true to a false so that we're now setting it to no longer be kinematic and then adding force in our direction times a number five let's try that again go back into unity we hit play drag our bird release him and well he falls down so the is kinematic change is happening but the force is not working he's not flying he's definitely not going far enough let's stop playing go back into our code and let's try cranking that number up let's add two zeros to it let's make it 500 save that off and then go back to unity again and remember it's ctrl s to save i use that hotkey constantly so just get used to it we'll go back into unity now we'll hit play again drag this bird oh look at that he is now flying away we now have a bird that somewhat works i'm gonna drag him over here to the right and see if he launches back that he launches towards his start position dragged him into the ground and he kind of went following let's move him around real quick i'm gonna click the bird i've stopped playing and i'm just gonna go back into grid snapping mode and just put them up here somewhere somewhere up to the left it loses negative 7.5 and one right now is the value that he's at hit play and i want to give him a quick drag and launch him and i think that's looking much better now before we go adding more functionality to our game i want to show you how to do a little bit of cleanup and optimization it's nothing major it's actually something relatively small that's not going to make a big impact but it's a good habit to get into and something that i want you to learn right from the start so let's open up our bird script one more time and let's take a look at all of these things where we're saying get component rigid body get component rigid body get component rigid body it's kind of littered throughout our code i see one two three four five places where we're doing that so far and this call while it's not terribly slow it's not the most efficient thing to do and generally when we're accessing components on a game object or in a script we want to save them off and only get a reference to them once we don't want to get a reference to them constantly now when we're doing it in a mouse up type scenario or in a start scenario it's not really a big deal again there's no technical reason that you need to do this and if you're running on a fast enough system there may never be a technical reason that you need to do this but it is a good habit a good practice to get into and it's called caching we're going to cache our variable references or our component references and make it a little bit easier for the engine or our game to get a reference to them and use them later on it also makes our code a lot easier to read because it's going to shorten things down and make it i think a little bit cleaner so let's make that change now the way that we're going to do it is take this little get component part right here on our line 12 where we get the rigid body and we're going to replace that we're going to replace it with a variable called rigidbody in our class to do that i think what we'll do is type it in a little bit different instead of having it auto complete and letting the editor do it this time we'll manually type it in so after line seven i'm going to hit enter and i'm going to type rigidbody2d and i'm going to put in a variable name so i put a space and i'm going to do underscore rigidbody2d and i'm going to leave the casing matching so i have an uppercase d and a lowercase r because this is one of my private fields in this class it's a variable that's only accessible from inside this bird class so now i have my rigidbody2d i'm going to double click on it and copy it with ctrl c and then i'm going to add in what we call in awake methods i'm going to hit enter after line 9 and type awake this is going to auto-generate it i hit enter right after so it auto-generated but i'm going to double-click on the private keyword and hit delete twice and then paste in my word rigidbody2d what i want to do here is get the reference to the rigidbody2d in this awake method i'll show you what awake means in just a moment first let's type out the code so say rigidbody2d equals getcomponent and then we'll put in the type rigidbody 2d and then open close parenthesis and a semicolon at the end to save that off so what have we done here we've saved off our reference to the rigidbody so that we don't have to call git component on it every time again git component while it's not extremely slow it's not the fastest thing ever it does do some searching through our object to find this thing and it's not cached it's not the fastest so what we're going to do now is replace all of our git component calls with this reference to the rigid body so double click on my rigid body ctrl c to copy it if it wasn't already on my clipboard then i'll find my git component rigid body instances here's one of them select the whole thing ctrl v to paste it and i can do the same here select it all ctrl v to paste it you can also do a find and replace if you prefer to go that way but i'm going to go through them manually so i can take a quick look at them all the next one is in on mouse up where we actually reference it three times we get the component three times in a row and we really don't need to do that so i'm going to replace this first one here just paste it in and then these two i'm going to cheat i'm going to hold alt hold click and then drag down here and look at that i can block select both of these sections this works in visual studio and writer and probably other editors i'm going to hit ctrl v to paste and look that it pastes over both of them pretty cool right now i'm almost done but i see the same thing with my sprite renderer i'm getting a reference to it here so i want to cache that as well and i'm just going to go up to the top do the same process here right after the rigid body i'll say sprite renderer let's see if i can find it here oh let's scroll down a couple more down arrow by the way to just go through these so sprite render underscore sprite renderer oh i gotta go back so i'm gonna hit control left arrow underscore end semicolon there we go get that sprite renderer typed out double click on it and copy it with ctrl c then paste and equals get component sprite renderer open close parentheses in the semicolon there we go so now we've got our sprite renderer cached it's still on my clipboard so i'm just going to find the two instances where we're using it here's one of them and paste it in and where was that other one right here and paste it in so now we are caching our variables let's scroll or zoom out a little bit and see what that looks like so we've got rigid body sprite renderer and a start position these two are components and this one's just a position or a vector that we're saving off i'm going to save my code and then let's talk about awake so the way game objects work or components work in unity is that they run through these automatic events or these automatic methods that are called because they're mono behaviors the unity engine calls these events well behind the scenes it manages when they need to be called and calls them for us the events are things like the awake that we just implemented start that's there by default the update the on mouse down on enable on disable on destroy and i'm going to show you right here on this page the execution order just a quick overview of what some of these look like or the order that they're in the first one that you'll see is a wake this gets called on all of our objects or all of our components first then on enable gets called on all of them then this editor method reset gets called you don't need to worry too much about that and then the start method gets called so the reason that we have an awake and a start and that they're different is so that we can cache things in the awake before they're needed by something else's start method so if we have things that are internal to an object or internal to a class like we have here with our bird where we want to get a reference to components that are on that object or on that game object we generally want to save those off in a wake and when we're using or what we want to do in start sorry what's better is to reference things that are like another object we haven't really done that yet but unity just by default gives us a start so that's where we started out with it doesn't really matter there's no technical difference between awaken start other than the execution order and really just it's there for us to be able to manage what things get called when you'll see more of that a little bit later on as our game gets more complex the other ones to note though are things like on enable which get called every time the object is enabled or toggled on and then there's an on disable that gets called whenever the object is turned off and then there's on destroy but we're not going to dive too much into these just know that you can look up the execution order just look for unity execution order to find more info about it now we're going to learn about a new type of variable a really fun one that allows you to do a lot of runtime customization or allows your designers to do customization if you happen to have designers or kids or a spouse or something else that wants to come in and play around with your settings and that type of variable is what we call a serialized field it's a variable that's going to show up in our inspector kind of like these other variables you see on the sprite renderer and rigid body order and layer gravity scale things that we've played with we're going to add our own to the bird and we're going to start with a launch speed or a launch force variable so i'm going to open up my bird script and i'm just going to double click on it in the inspector this time to open it and then we're going to go up to the top of the script and i'm going to zoom in a bunch so that the text is nice and big now i'm going to add in a new line here and then we're going to start with the square brace this is the key right next to p and it's uh right around the enter key right by our squiggly braces without a shift get my square brace right there gives me the open and close and i'm going to type in a capital s and serialize field just like that serialize field and i'll go to the end or over to the right one space and add another space and i'm gonna give this a type of float this is gonna be a floating point number or a number that can be positive or negative very large with a decimal so it's going to be a float and then we'll give it a name and just like my other variable names i'll start it with an underscore because it's going to be private it's not going to be accessed from outside this class i'll say underscore and i'm going to call this launch force l-a-u-n-c-h and then a capital f-o-r-c-e you're going to start to see the value of this camel case in just a moment now i'm going to give it a default value so i'll assign it with an equals and let's just assign it to 500 because that's what we're using right now so i've got a launch force value set to 500 and i'm going to add in one extra line just because i like to keep my serialized fields separate from my other fields that are not serialized it makes it a little bit easier for me to read the code that's all so i've got my launchforce variable here but i'm not actually using it so i'm going to double click on it and hit ctrl c to copy it get it on my clipboard and then scroll down to the part where we add 500 force double click on that and ctrl v to paste it in so now what we're doing is using a variable amount of force the amount of force is going to be adjustable in the inspector watch as i save my file with ctrl s go back in the unity and look at the bird after it reloads there's a little indicator in the corner there we go it finished reloading and i now have a launch force value here and i can adjust this value i can click on it drag it to the right drag it to the left or i can just click right in here and just put in whatever value i want by typing i'm going to hit play and try launching my bird now i've just turned the value down to 200 let's see what it does so i pull it back and now he does a very light little launch let's try cranking that up to 1000 now 1000 and i'm going to drag him and remember his start positions over there so we'll get him right about here and oh he just goes flying off let's try again oh he's actually going flying back the other way he's going nice and fast though i can see that my force is working and now that our bird can actually fly i think that it's only fair to give you some things to shoot your bird at or launch him at so that you can make them knock over before i make you write any more code first though i'm going to crank his launch force back up to 500 while i'm not in play mode and save my scene with control s then i'm going to go into my art and go to the props folder and let's start with a couple of crates i'm going to zoom out my scene view with the mouse wheel here and right click and drag to pan around i'm just going to place a couple of crates over here that i can launch my bird out and see if i can knock them down or not so i'll take crate one right here click and drag it into my scene and make sure i've got snapping mode on and if you can't see snapping mode it's not on make sure that you clip click over to that global mode and turn snapping on so that it's lit up there we go i've got it snapping into position now i'm going to duplicate that one well you know let's not duplicate it yet i have a crate here and i want to be able to knock this crate over if i just hit play right now and launch my bird at this crate he's not going to knock it over watch he's just going to go oh okay let's try it again let's try um cranking up that speed a little bit more on this bird i'm gonna put it up to like a 700 and then launch him really really hard at the crate and watch he's still not gonna knock it over even if he does get to it there you go he just goes right through it because our crate doesn't have any physics data it doesn't have a collider it doesn't have a rigid body so i'm going to go to my crate and we're going to start by adding a rigid body 2d so i'll just start typing rig notice that i can just kind of filter down i don't have to go through the menus to find things the menus are great if i want to find and discover new stuff but if i know what i want to find i can just type it in and get it quicker so there we go rigid body 2d the other thing i want to add is a collider now here i probably don't need a polygon collider i have well really a box it's a square it's very simple very straightforward so i'm going to go with a box collider 2d that's how we do a simple square so add a box collider 2d notice that the green outline matches up perfectly now let's zoom out and let's launch the bird one more time at this crate and see if he actually does something to it we go pull it back and he goes flying and well he kind of nudges it there we go not bad it's better than what i was getting before now he's not going through it anymore so i'm going to stop playing i'm going to take my crate and i'm going to do what we call duplicating it i'm going to copy this crate so that we have multiple instances of it and we can stack them up so to do that i'm going to select the crate and hit control d if you're on the mac it's command d but control d now i have crate one and notice that it put these little parentheses in a one this just means it made a copy of it and tried to give it a unique name and it gives it a one if i do it again it's going to be a two and so on so i want to move this crate one and it actually selected it automatically so i can just click and drag it up and look at that now i have two crates stacked i'm gonna hit control d again got three crates stacked after i move it up ctrl d again move it up four control d i've got five let's go to six hey let's go all the way up to seven and be crazy right now let's hit play and try launching our bird at this stack of crates so take the bird pull him back and launch and like that he almost knocked him over oh you look that just came so close i'm gonna stop playing crank up my bird's power even more i'm gonna give him like a a 1000 value there hit play one more time and just see if i can launch him right through these things pull it back launch and there we go he now knocks over the boxes i like knocking over all these crates but i think that they look really kind of similar and it looks a little bit boring having them all match exactly so let me show you a real quick trick where we can add some randomization before we even look at adding in the create 2 sprite so i'm going to go select one of these and hit the flip y button look at that it now looks different just select another one hit flip x i'll select this one and hit flip x and y go flip at y on this one and maybe just flip x on another one of these now i've got a little bit of variation there and variety and it's a nice little trick that you can use in 2d when you have sprites like this to just swap them and move them around and kind of reuse them so you don't end up having to create them in all different directions and things now that i've got them kind of set up though let's see how i could swap out the sprite with a different one too maybe i want to have let's change this one and oh maybe this bottom one to not have a cross beam in them so i've selected them both by holding control and clicking on them i could also click over here hit create one hold ctrl and select this crate one number three right and get them both selected and then i want to change out the sprite value here to do that i have two options i can either hit this little search box or search button which will pop up a select sprite option where i can just go select the crate that i want so here i'll drag this down and click on it and you can see that it actually swaps them out right there that works fine that's one way to do it and it's pretty easy to do another way to do it that sometimes catches people off guard and might be an issue but it's worth learning and understanding is to click and drag into the sprite field so i can click drag and then release now what people often do and mistake this for or mess up with this is they'll go over here they'll click on it release and now the inspector is showing data about this sprite instead of about the two selected objects those objects are no longer selected so i got to go re-select them and then they go oh here i clicked on it now look i did it again and then they start to get really frustrated so if you start to struggle with that just remember hold click don't release and then drop it or hit the little search box and get the pop-up it works great if you don't have a lot of stuff in there and the pop-up has a search filter too so you can use that to find things if you're having a harder time so let's save off our scene one more time hit play and go knock over these different looking crates see if they go flying there we go look at that now that our bird can fly and hit things and knock him over it's important that we set up a way to reset our bird so that he can go back to a starting position after we've launched him we can launch maybe a couple of birds or just relaunch the same bird that's the technique that we're going to start with just relaunching this same bird so after he collides with something or hits something we want him to go back to his starting position we're going to open up the bird script and add in an on collision enter 2d method now so we'll open up our code and i'm going to go right down here to the bottom right below our update in fact i'm going to oh yeah let's go right below updating that in two lines so i'm right here on line 54 and then i'll zoom in and we're going to type in a special method called on collision enter 2d now i didn't type in the casing because i know it's going to auto-complete but it is case sensitive if you end up typing it so i'm going to select the on closure and enter 2d let it auto-complete and there we go we now have the method i'll double click the private and delete that because again i don't need it and this is what it looks like on collision inter 2d with a collision 2d type of object that's the parameter that will get passed to us named collision so what's going to happen here is whenever our sprite or our bird collides with something this method will get called we'll get passed in a special parameter that has some info about the collision telling us what things we hit what angle we came in at how hard we hit it at or how hard we hit it what that velocity was like and we can use that data to make some decisions really all we care about though is hey if we hit something we want to reset our bird so let's think about what our resetting the bird would look like to reset our bird we want to go back to our start position and we just want to set ourselves back to being kinematic or not moving anymore so let's scroll up and see what that looked like remember when we set is kinematic to true it's just like this and here we're caching our start position so we essentially want to do the opposite of this set our position to our start position and then set the kinematic to true so here's what i'm going to do i'm going to cheat i'm going to select these two lines hit control c and copy them then i'm going to scroll down here and paste them in in my on closing enter right in between the braces here on line 56 now i'm going to change this i don't want to re-cache the start position when i hit something in fact i want to just move my position back to the start position so i'll hit shift delete get that on to my clipboard delete three more times hit end to go to the end and left arrow equals and paste there we go now i have position equal start position there's a missing space here so i'm going to click and add it make it look a little bit cleaner and it's now getting set to kinematic so if i save this off now when i hit something my bird should instantly reset and become kinematic and let's try it out and see if that works we go into unity let it build and if we're running into errors by the way if you're having an issue and it's not playing make sure that you click over to the console here there could be an error message or a problem something that's showing up and usually what will happen is you can click on that and find the issue and fix it now i'll show you what that looks like real quick in fact let's say that i added in an extra equal sign there and saved that off now there's an error here i've got a red underline maybe i didn't notice it before i switched over to unity come into unity and it says hey only assignment call increment decrement await and new object expressions can be used as a statement you might think okay i don't know what any of that means i'm lost i'm confused what really matters is right here dot cs 56 comma nine it means it's in my bird file on line 56 at position nine where it's finding it and really what i care about is it's on the bird file line 56 i should be able to find it from there i can double click on it and go okay here's the error and then kind of look at it and see if i can figure out what's going on there if you're not sure what the error message means you can always google it let's fix that save it go back into unity and now try out our bird see if he does his reset all right here we go we pull drag and release bam bam bam okay so he's going back and he's just constantly getting reset he's not completely stopping though and in fact he didn't keep getting reset he just kept launching over and over i guess so let's fix it up so that he stops moving we set him to is kinematic so that our system won't move him automatically or won't control his velocity we actually need to set his velocity down to zero as well to stop him to do that we'll go back into our code and right here after we set is kinematics to true we'll add another line say rigidbody2d dot velocity there we go equals and we can use this built-in vector called vector2.0 which is just a zero for the x and a y so it's just going to stop the thing completely let's save and go try it again we pull the bird back we launch him he hits the ground and he resets completely he doesn't reset his rotation notice that but he does reset his position and he just kind of stops he keeps spinning around that's totally fine we'll deal with that later but beautiful but he is resetting every time so let's stop and then let's go make the bird stop spinning to do that i'm going to go click on the bird and then we can just go into our rigid body 2d and expand out the constraints section let's collapse this and expand out constraints right here and we have the option to freeze position or rotation i'm just going to freeze rotation for now so that our bird doesn't keep spinning around and rolling or anything let's save our scene one more time and try launching him again there we go he now resets and looks pretty clean our current reset code happens a little bit too quick for me i think so if i pull back and launch my bird he kind of resets before he can go flying through things and i don't want that to happen i want him to reset after a little while i want him to hit something and then maybe wait three four five seconds and then do his reset so we're going to implement what we call a co-routine or a way to allow us to wait a period of time and then execute some code or to run some code over time is another good use for code routines so i'm going to stop playing and we're going to open up our bird script again double click on it to open it and what we're going to do is modify our on collision enter we're going to take the code that's in here we're going to pull it out into another method that will invoke as what we call a co-routine or a thing that runs over multiple frames to do that i'm just going to start typing a new line right here after 55 and i'm going to begin with the start co routine method this is a built-in method in the mono behavior so we have it in any of our components that implement mono behavior as or base class or anything that we created as a new script like that like we've done for this one and now we do open parenthesis and we need to give it a name and i'm going to call it reset after delay with a capital on each one of the words because this is a method not a variable method names should always be what we call pascal case where they have an upper case letter for the first word and for every word after that notice that for update on collision inter and all of the built-in unity methods they all do that so now i need an open and closed parenthesis again and that's inside of these other parentheses so we have an open here another open a close and a close you'll always notice that these should kind of match up you'll end up with the same number of opens as closes for braces parentheses square braces and everything else now i'm going to hit semicolon and close that off now it gives me an error here saying that the name reset after delay does not exist in the current context and it gives me the option to show potential fixes or i can click on this little box here and hit generate method i'm going to hit generate method but it's going to give me the wrong thing and i want to show you that because it gives you the wrong thing every time and it causes problems so i click on it and well you can't see what it did but if i scroll down there we go it generated this private method that has a string return type called reset after delay and then on line 65 it has this throw new not implemented exception that's just a default thing whenever we generate a method that's an error that will pop up saying hey you forgot to actually write the code for this part and it assumes hey until you write the code you probably want the error message saying hey don't forget to do this you didn't create it for no reason so we're going to delete line 65 here we don't want to do that we're going to implement it but we also need to change this string string is not the correct return type for a co routine it needs to be an i enumerator now it's also possible to accidentally put i enumerable in there if you see a b in there you messed up just change it and switch it over to i enumerator it's still going to be red it's still going to give an error because we haven't finished implementing it i'm going to remove the private keyword here double click and delete twice and then we'll go inside the method for reset after delay what we want to do here is wait we want to do a yield or a wait for seconds and we're going to wait for i think three seconds to do that we're adding this command called e yield d then return r e to u r n with space between them and then new and don't worry about the syntax it looks a little bit weird and confusing you're going to get used to it when you write code routines you do a yield return new and it's almost always wait for seconds or you'll return null you'll see that later on though for now though it's yield return new wait four seconds with capitals on each letter or each word and then an amount of time and i'm going to put in three close parentheses and a semicolon so this is just going to wait for three seconds now after three seconds the next line of code will run now for the next line of code i really wanna set all of these positions back to zero so i'm gonna select them all i'm gonna cut them to cut them we can either right click and hit cut or on a regular keyboard it's just control x or shift to delete now it's on my clipboard i'll hit ctrl v and paste it right down there and then i'll go up to line 58 and hit shift delete to remove that entire empty line there save it off go back into unity and play and let's see what happens i launch i hit a box hit some stuff and there we go after a couple seconds he resets now it's worth noting that it it resets after my last collision so every time i collide it's restarting that method and the reset's happening at the end of the last one there we go though it's resetting after a delay and i can actually see my bird landing there and hitting things so when i start to have enemies i can see them getting hit as well now on the topic of monsters i think it's about time that we put one in and give our bird something to hit some target to hit and progress throughout our game so we're going to go into our art and monsters folder and let's take the open eyed version of the monster click and just drag him into the scene i've got my snapping on so i'm going to move him around and get him into a good position right behind these blocks then we'll add some components to them first we'll add a rigid body 2d so that he responds to physics and then we'll add a polygon collider 2d now i'm going to zoom in well let's expand out the polygon collider and then zoom in and see it looks pretty good i think it matches pretty well remember if it's collapsed we won't see that green outline i've got my polygon collider my rigid body my enemy there he's almost ready to knock over right let's go shoot our bird at him and see if he goes flying there we go we knocked the bird and he tips over the the monster kind of cool but he should really be killing the monster right so let's start by making our monster die or let's not really start but continue by making our monster die when he gets hit by something to do that we're going to give our monster a new script we're going to create one specific for our monster so i'm going to go into my scripts folder right click hit create choose c sharp script and type in monster with a capital m m-o-n-s-t-e-r hit enter there now i've generated my script and the next thing i'm going to do is go select my monster and assign that monster script to him i don't want to forget to do that later and wonder why my thing's not working because i forgot to attach it now i've got my monster script on my monster and this is going to deal with collisions just like we did with the bird when our monster gets hit or hit something we may or may not want to kill him now we don't want to kill him every time because he's going to fall and hit the ground we probably don't want that to kill him but we do want to kill him if he gets hit by the bird or maybe if he gets hit by a crate from above or gets hit by anything from above so let's start by just making him die to the monster to the bird not the crates at all so we'll open up our monster script by double clicking on it this should pop up a visual studio instance with a new script that looks just like this where we have our monster and it has a start and update again if you have problems opening it up you can always go into edit and then preferences and then go to external tools and try regenerating your project files or just restart unity that tends to fix things or restarting visual studio all right now we're in our monster script and what we want to do is first just delete start and update i don't need either of these so i'm going to select them all from line 7 to 17 and hit delete now i just have an open and closed parenthesis around my class this is the bare minimum that i need for a class other than i don't need these two grade outlines but we're going to leave those alone for now zoom in a little bit and we'll start typing our code we're going to add an on collision enter 2d so type in oncol and hit the on close and enter 2d version of it right there and then double click on private and hit delete if you have to type it out again there's the text for it just type out on collision enter 2d make sure that you match the casing now what do i want to do well the next thing i want to do is check to see if we got hit by a bird so let's check to see if the collision was with a bird because if it was with a bird we'd definitely die instantly so i'm going to say bird bird equals collision dot game object dot get component don't worry i'm going to explain what all this means in one second and then an open or the less than the one next to m and then bird so we're giving it our type go to the right and do an open close parenthesis and a semicolon so what are we doing here we're looking at the collision and the collision gives us a game object of the thing that collided with us so that's this reference to the object here that's this thing right here so say we hit with a bird we're going to get a reference to this game object and we're going to say hey on this game object get a bird component now if there is a bird component we'll get a bird here this will be a valid value it will have a result in it if there's not a bird on there we'll get back what's called null or nothing it just means that it's a reference to no bird at all there's nothing there at all it's totally essentially like a zeroed out nothing value so if there's nothing there it will be null if there is something there it will be not null so what we can do is say if bird is not equal to null so that means that we do have a bird i'm going to hit enter here and i'm not going to add braces after this statement i'm going to say destroy or no let's not do a destroy we'll say game object dot set active and then we'll do an open parenthesis past and false this is going to take our current object the monsters game object and set it to not active or uncheck the checkbox in it you'll see that in just a second let's hit play go back into unity i'm gonna move my monster to the front of the boxes right here so that it's easier for me to hit him with my bird and then i'm gonna move my bird a little bit to the right too so it's easier for me to hit him hit play and then i'm gonna launch my bird right at that monster look at that the bird has disappeared if i go select him you see that he is actually not gone completely he's just turned himself off his active got checked unchecked or set to false so now we have a bird that can hit a monster but we want to make our monster also die to crates falling on top of it so let's extend this out a little bit more i'm going to move this guy back to the right and then we're going to go back into our monster code the collision actually has a little bit of extra data here it doesn't just have the game object it has what we call the contact data or the data about where we hit what angle we hit at what velocity we were at if we were coming in from above below if we were coming in fast or slow all of that kind of stuff so what we want to do is set up a little bit of extra determination on whether or not we should die from a collision so i'm going to modify this method a little bit we're going to add in some code right here on line 8. and we're gonna say right here if should die from collision so i've typed out the full words with capitals on each one and then we'll do an open parenthesis and this doesn't exist we're gonna have lots of red lines we're going to pass in the collision so type in collision or co and just hit enter and let it auto complete then i'm going to hit end and hit enter and go to the next line so if we should die from the collision then we're going to run some code inside of parentheses so i hit shift and the key next to p to get my parentheses oops there we go clear that out and hit enter and then i'll just say die open and close parentheses so we're going to say if we should die then do the dyeing now neither one of these methods exists so i'm going to select the die one and i'm going to hit alt enter or click on this little option here and hit a generate method monster.die it's going to go down here and create a private void die that's not implemented that's okay i'm going to delete the private and then i'll delete the not implemented and we'll what we'll do is take the setting active to false and make that our death so i'll take gameobject.setactivefalse hit control x and then paste it down here with ctrl v and then we need to implement our should die from collision don't worry we still have some error code down here we haven't finished fixing this piece up we're going to do that right now we take our should die from collision and i'm going to hit alt enter this time and then hit generate method should die from collision this one did something different now we have a method that doesn't have a private void it has a private bool it's going to return back a true or false value and that makes sense because the method is named should die from collision that's either going to be a true or false statement it should die or it shouldn't die so what we need to do is implement a method that will return back the value true if it's a thing that should kill us and it'll return back the value false if it's a collision that shouldn't kill us so we'll start by taking lines 15 and 16 and we're going to move them into this should die so i've selected them both i'll hit control x and i'm going to go down here and hold shift and go down down down and hit delete and just clear that out i want to get that all cleared out so that our on collision inter 2d is a valid method that works it has an open it has an if statement to check if we should die from a collision where we passed in a collision and if that's true we die that's the end of that method now let's implement our should die from collision i'll delete this part right here the throw new not implemented exception i'm going to delete it a shortcut way i'm going to actually hit ctrl v and paste in my other code so let's think real quick about what we want to do here so if our bird is not equal to null should we die from the collision well the answer to that is true so what we want to do is return true so now this method should die from collision if there's a bird in that collision we'll return the value of true and this if statement will be met and we'll die now what if the bird is not null or the bird is null so that we don't hit a bird and nothing else has been a valid reason for us to die yet what should we return well by default we want to return false so add in two lines and say return false so if we never meet a condition that means that we should die or return false otherwise we will exit out here and return true instead the way that this code works is that after we hit a return statement we no longer execute anything else in that method we exit out and come right back in so this will come back as true and then it'll die now since we've made another code change it's a good idea to jump back into unity and go try it out and make sure that our change didn't break anything we didn't make a typo or make some sort of a mistake there so grab my bird and launch him out and look at that as soon as my bird hit the monster he died not before that but right when he hit it so i'm going to stop playing and then let's start thinking about other ways that our monster could die one other way that our enemies should be able to die is from a crate just falling on top of them so we're going to extend out our should die from collision method a bit and make it so that it returns true if a crate falls on top of them or if anything falls on top of them so let's open up the monster script and right here after line 20 where we return true if we hit a bird and add in two more spaces we're going to add in some code here that checks to see the normal value of the collision which is the angle that we came in at or the vector that represents the angle that our collision happened at and what we want to check there is the normal's y value if it's less than negative 5 it means that we came in from pretty much above we came in from about a 45 degree angle or above i don't want to dive too deep into how normals work because it's a somewhat complicated and a little bit confusing topic but i've done a bunch of videos about it so just look up normals in unity and you can see quite quite a bit of info about it on my channel or anywhere else for now though let me show you what the code looks like and show you how it works so what we're going to type in is if and we're going to use that collision object this one right here collision dot contacts now contacts is actually an array or a collection of different points where our object or our collision happened so imagine that i've got my finger here and it hit my other finger and it hits right there there's like one specific point right where they started touching it's going to give me that one point it's a little bit better with something like an id or a pencil or something fine point i don't have anything short sharp around me but imagine just got that one little point now sometimes we could hit in two spots imagine we hit in two spots at the exact same time that's why contacts is a collection or an array because sometimes we could hit at two places we almost never need to deal with that most of the time we're dealing with collisions with one contact and that's all we care about so we're going to deal with the first contact here to do that we put in a square brace and a zero that's going to read the first contact or get the contact at index position 0 which is the first position in an array or a collection now what do we want to read on that contact we want to read the dot normal so we add in a dot normal and that's a vector 2 with an x and a y we want to check the y value so we put dot y and we want to check if it's less than negative 0.5 that means that it's coming in from above if it's at zero it's going to be right equal so we come in like from the side the y is going to be zero if it's coming in from above it's going to be a negative number and the higher above it is the lower negative it is if it's directly up and down coming straight down it's going to be negative one if it's coming from below it's going to be a positive one straight up and down so this point five is kind of that 45 degree angle on our normal so as long as it comes down from somewhat above then we want to count it as a fall on top of us so what do we do if something falls on top of us if the collision's contact was from above we'll return true there we go that's all we need this one little line well two little lines should allow our boxes to crush our enemy so let's try it out we'll hit play grab my bird launch him over the box and look at that the enemy gets crushed by a crate so if you've been experimenting with your bird at all or maybe you let somebody else play or let a kid play you might have noticed something particularly that i could just drag my bird over here and tap that monster and kill him not the behavior that we want right so let's fix that and let's limit the way that we can drag our bird we really only want him to be able to drag off to the left and we want to limit how far away he can drag as well so we're going to stop playing and go open up the bird script and then find our on mouse drag section here we're getting the mouse position and right now we set our position of our object to that mouse position so we use our camera dot main dot screen point or screen to world point passing in our input.mouse position to get the position in the world for our mouse and then we set a position here with a new vector 3 using the x and y and we ignore the z because we don't want to move our character up along with our camera that's at negative 10 on the z let's change it up a little bit our mouse position is going to stay the same we're going to leave that as a vector 3 but we're going to start using vector twos and we're gonna start clamping our position of our character so we're gonna add in two new lines here i'm gonna add in all three lines and then go up to line forty seven so we've got a little bit of space here and i'm gonna create a vector two called desired position so say vector two desired position there we go spell it right and i'm going to assign it to the mouse position this is actually gonna give me just a two dimensional vector that doesn't have that z value so it'll take the x and y from the mouse position and ignore the z because we can't store a z on a vector 2. now i want to add a check to see if that desired position's x value is greater than my starting x value so say if desiredposition.x is greater than underscorestartposition.x so if that's true it means that i've got a position that's to the right of my starting position because values go to the right they go up or as values go to the right they go up that's what i mean to say there so if it's higher it means that it's to the right of my start position so if that's the case i'll just say desiredposition.x equals startposition.x that will clamp my position from going too far to the right or past my starting position although it doesn't quite do it yet because we're not using our desired position right now we're still setting our transform position to this combination of mouse and existing position i'm going to take this line and hit delete instead what we're going to do is set our rigidbody's position so we'll say rigidbody dot position or rigidbody2d dot position and this takes a vector too if i put my mouse over it you can see that we'll assign it to desired position save that off go back into unity and let's see if we can drag to the right anymore we're no longer able to kill our monster with that little cheat so here we go take it and he will not go to the right i can go to the left anywhere i want but he won't go right and i can still drag and launch him it's looking quite a bit better let's add in a max distance now so that he goes in more of like a circle at some maximum range from the starting point to do that we're going to add in another variable we'll set up another distance type variable instead of a force variable in our bird script so we'll open up our bird and i'll go up to the top i'm going to hit control and home it takes me right up to the top of the file in visual studio and then right after launch force i'm going to hit a new line and add my square brace and serialize field again there we go let it auto complete go to the right and add a space here i'll make it a float because this is going to be a decimal point value or a value that can have a decimal point in it i might want to go 1.5 meters or 2.5 meters or something and i'll call this underscore max drag distance there we go that's always hard making sure i spell things right then i'll assign it a default value of five so five meters seems like probably too far i might have to pull it in a little bit but i want to start with something relatively large all right i've got my max drag distance variable there and i want to implement it now make it actually do something so we'll go down to our on mouse drag and after we do the clamping of the position let's figure out how far away that desired position is from the starting position there's a built-in method to allow us to do that called the vector2.distance we're going to get a value back that's a floating point value so just a number that has a variable floating point decimal or floating decimal point it can be a number that's got smaller big numbers with lots of decimals it's a number with decimal points float float distance equals vector 2 dot distance pretty easy method right let me do an open parenthesis and we'll give it our desired position and our starting position with a comma in between those two parameters so we separate our parameters with comma and this is going to give us back the distance in meters from this position to this position return it back as a floating point value named distance if the distance is greater than our max distance then we want to do something we want to adjust our desired position again so if our distance is too far then we'll say hey let's not use our desired position or let's maybe move our desired position to the furthest point in that direction right so we're going to need to get the direction which we get somewhere else remember we get our direction right up here so we'll need to get the direction again and then we'll need to figure out the maximum point away in that direction so to get our direction remember we need to go from our starting point to our current point so let's add in some braces here after our if statement so we can add a couple lines of code and then here we want to create a vector 2 for that direction so we'll say vector 2 direction equals and we're going to use our desired position minus our start position this will give us the direction that we're dragging in the character away from our start position towards our desired position we need to turn that into an actual direction though and not just a vector with magnitude so we use the direction dot normalize again there we go and add our semicolon with the parentheses so that our direction becomes normalized and then we finally need to change our desired position to be the point in that direction at the maximum distance so our desired position is going to say desired position equals and now we can't just say our direction times the value because that would give us a value that's somewhere around zero if we just put in direction which imagine like if it's vector three or vector two dot right it would be a one on the x and a zero on the y if we did that times the multiplier it would just be at five and we don't want that we want it offset by the start position because this is a arc around the starting position so we're going to change this up and we're going to make it desired position equals and we'll use start position plus direction times max drag distance now we want to do the direction times max drag distance together and it's already done like that but we're going to make it explicit and just add parentheses around it to show the order of execution here direction times max drag distance happens this gets multiplied and then added to our starting position vector now the other thing that i want to do is move our clamping down below so that if we move our character in the direction that's off too far we don't snap them over there after we've tried to do our clamping so we need to take lines 49 and 50. i've selected them both and i'm going to hit shift delete or again control x and then that'll cut them i'll go down below line 57 and hit ctrl v and paste them in i'm going to go up here to line 49 and hit shift delete to just clear out that line and i'm going to do it one more time on 47 to clear that line out too save my file and then i'm going to go back into unity and check out my awesome bird so we hit play give him a drag look at that he clamps right there he's still a little bit too far i can't go to the right he doesn't snap to the right or anything but he's looking good let's release him and then let's change those values i want to turn that drag distance down from five to maybe two let's see what that looks like i haven't stopped playing and look at that i can clamp him now at a better value something that's a little bit closer i think i want to try maybe like a 3.5 so i'll change it again go to 3.5 wait for him to reset and click and drag and yeah that's looking a little bit better let's release him and see how he launches all right things are starting to look good now now you might have already added more monsters and if you have that's okay but we're going to make a couple changes to our monster script and make him a little bit more powerful or at least make him die a little bit better we're going to add some particle effects to him so that he plays a little bit of a poof effect make it so that he can only die once and make him change his sprite to do that we're gonna find our monster purple open and we're gonna open up our monster script by just double clicking on it what i want to do is set it up so that our monster can switch to the dead eye sprite or the closed eye sprite when he gets hit or dies so we're going to make that change first actually first i'm going to find this private keyword double click on it hit delete twice and clear that up just so that it matches the rest of our code then i'm gonna go up here to line seven and hit enter enter and then go up an arrow or hit up arrow once and go up to line eight i'm gonna add a square brace and put in the serialized field and then go over to the right and we're going to add in a sprite with a capital s we're going to call this dead sprite with a capital s and a lowercase d and an underscore at the beginning this is going to allow us to assign a sprite to the monster in the inspector we're going to be able to set what sprite we want to show when he's dead and then we'll use this in our code to change it over when the character actually dies so i'm going to save and then i'm going to scroll down to the part where we actually die in this die method instead of setting our character to not active i'm going to do something a little bit different i'm going to get component and get our sprite renderer so i'll use the less than to give it a type and put in sprite renderer close that off with greater than and then open close parenthesis and then put a dot and type in sp and you'll see that sprite is one of the options this is one of the things i can change so i'll say sprite equals because it'll allow me to assign it and i'll put the underscore and i can see my dead sprite right there add a semicolon to the end and i'm done i don't want to set this thing to inactive anymore so i'm just going to put two slashes at the beginning to comment that line out i could also delete it but commenting it out just reminds me oh yeah i was doing something there i might want to do something later with it so i'm going to save that off make sure that the little star disappeared there and i don't have one on my bird come back into unity let's go try killing one of these monsters or killing this monster all right so i hit play and we'll drag him and launch and look at that he just kind of disappeared oh what's going on oh hey look at that our sprite isn't actually set so our monster's there and if we expand out our sprite renderer it's either just write a set to none because the dead sprite's not set so i'm going to stop playing we're going to select a dead sprite i can hit the little search box here and then go over to my pop-up and choose the closed i one i'll save my scene with ctrl s hit play again and let's see if he closes his eyes this time there we go now he's dead now we're going to talk about particle systems we're going to add a particle when our character dies and particle systems are one of those things that might sound complicated or like you need some magical art skills we're not going to make a beautiful particle system so don't worry about it not looking great or you're not being able to make something that's amazing we're just going to make a simple one so you can understand how they work how to fire them off and how to fire off some other ones usually when it comes down to using real particles in the game i prefer to find artists to make them for me or buy them from places like the asset store so i'm not going to teach you how to make pretty ones just how to use the ones that you can make and how to make your own so let's get started by creating a new particle system i have my monster purple open selected and i'm going to right click on him and hit effects and particle system this is going to create a new particle system that's a child of my monster purple open i could also alternatively add it as a component right here i'm just going to let it be a child because a lot of the time when i make a particle system it ends up being a child of the object so that i can put multiple particle systems together and offset them and change them so you can see here that this particle system just shows a bunch of little white poofs flying off to the sky that's kind of close to what we want i just want a little puff of little white dots or smoke or clouds or something flying off of the guy whenever he dies so we're going to change this one up to not just constantly loop out these particles and instead do like a one-time shot the first change that we'll do is uncheck the looping checkbox now you'll see that it's no longer spamming them and i can hit play and just fire off some particles when i want they'll last for what is it five seconds and then the particles will disappear after five seconds that's not the effect that i want either so we're going to expand out the emission field or the emission tab or section and make some changes here all of these sections down here by the way can be expanded they're just not all turned on by default so expand out emission and you'll see here it has a rate over time so here it's doing 10 particles per second over a time of five seconds that's the duration there we'll scroll back down i'm going to change this to zero and then we're going to add a burst i'm going to leave it at 30 and leave the probability in cycles and everything alone and then hit play like that it's now one single burst of a little poof of smoke kind of closer to what i want but i want them to move faster and disappear quicker so let's make a change so that they move faster and disappear quicker to do that i can change my start lifetime and i'll turn that down to something like two so it'll disappear after two seconds that there they go they'd start to disappear then i'll change the start speed and i'm going to crank that up to something like 10 it was at five doubling it seems good there we go now i want to change something so that they'll start to fade away actually let's start them bigger first right now they're starting at a size of one let's start them at a size of two and hit play look at that now they're a big poof now i want to make them shrink and change over time to do that i'll go to size over lifetime and i'm going to expand it and then check the box to enable it now i can click on the size graph right here this is actually a chart or a line or a curve really is what it is if i click on it it'll allow me to modify the size over time here it's starting off at a size of zero and scaling up to a size of one so if i hit play they'll start small and scale up to big i want to change that and invert it so i'm going to take the left side here with this red dot drag it all the way up to 1 and take this right side and drag it down that didn't make it perfect though i can just click on this little line right here to give myself a nice linear curve without having to worry about how to edit curves in the editor let's try again i'll hit play there we go and now they just kind of fly off and train let's restart it one more time looking pretty good i think i want to shrink the lifetime of these even a little bit more though so let's go down to maybe 1.25 one and a quarter seconds and then i think i'll leave the speed alone let's try that restart there we go they kind of fly and just poof away it's not the best effect in the world but i think it looks pretty good and it'll work now there is an issue here though because my particle is showing up behind my character look at that if i play it you'll see that the particle's there but it's behind the character and i want it to show up in front of him so i'm going to collapse the emission and size over lifetime sections and expand out the renderer scroll down and look here we have a sorting layer id and an order and layer option let's just add a layer for our particles so i hit sorting layer id hit add sorting layer hit the plus and type in the word particles i'm going to put in a capital p so that it matches my casing everywhere else then we'll go back to our particle system because our inspector is now showing tags and layers so i got to click on the particle system scroll back down and then go actually assign our sorting layer so we'll go choose particles hit restart and look at that now it's on top of the character that's in the default layer everything is looking pretty good except if i hit play watch what happens he shoots off the particle right from the beginning that's not what we want we want that to happen when he dies so let's scroll up and uncheck the play on awake method or play on awake checkbox this would make it just automatically play when the character starts or is awoken i'm going to save and then we're going to go in and add the code that will make our particle system play to do that we'll go to our monster and i'll just double click on the script right here and we're going to scroll up to the top and add another serialized field the serialized field is going to be for a particle system so say serialize field we'll add particle system and add an underscore particle system so this is going to allow us to assign a particle system to this field and then we'll be able to control when we play it in our death code so i'm going to double click on my particle system and copy it make sure that you match your casing so that the names show up right in the inspector and then i'll scroll down to the die section of our code add in a new line after the setting of the sprite paste in particle system and put in a dot and the word play within open close parenthesis and our semicolon that will force our particle system into play when we die let's try it out go back to unity we can't hit play just yet because our particle system isn't assigned so i need to assign it to do that i need to click don't release hold and drag and drop it right in there now that it's in there i'll save with control s hit play and let's see if i can kill this monster and see some particles go flying look at that he's got particles they look pretty good except they're rotating and spinning with him that's a little bit weird right we probably don't want that behavior so let's stop playing and go back to our particle system there's an option in here for simulation space simulation space means well it's got two options let's talk about them real quick local and world local simulation space means that these particles will move relative to their parent object or to the game object that they're on so if that game object moves these particles will move with it if it rotates these particles will rotate with it you might think why would i want that that seems stupid because obviously it looks weird right here but that's the case for a lot of particles imagine you've got a flaming sword you don't want the particles to just stay in the world you want them staying on the sword constantly or you might have like a glow effect of particle that's around a thing you want that to stick to the object it depends a lot on the specific scenario if it's some kind of thing that's supposed to just kind of float off then sometimes you don't you then generally you don't want to use local but if it's the thing that stays attached and tight you usually do want to use local so we want to switch it to world so our particles will simulate in the world and they will ignore our local transform our local position and rotation if we try that little change hit play and go kill him we should see a much cleaner looking particle death looking good other than he dies multiple times now we're going to add a little bit of code to prevent our character from dying multiple times so that our monster won't play that particle every time he gets hit to do that we're going to open up our monster script and in our should die from collision we're going to add another check so before line 21 we're gonna add in two new lines and i'll put it in right here if underscore has died then i'll go to the right add my closing brace add a new line and say return false so if we have died we should not die from a collision now has died is not something that exists that's why i have an underscored red little squiggly saying hey this doesn't exist fix me if i hit alt enter or click that little thing the little light bulb there i can generate a variable for it i'll click on it and hit generate field and that's going to create a field up at the top or a variable for our class up at the top i'm going to scroll up or i can hit f12 to go to it that's the shortcut on the hotkeys so here i have a private boolean value which is a true or false named has died double click on private and hit delete twice to get rid of my extra keyword and add an extra space just so i can separate out my serialized fields from my regular fields okay so has died how is that going to work well when we die we'll just set it to true so then we should never die a second time to do that we'll go to our die code and right here at the beginning we'll just say has died equals true we can save that off go back into unity try killing our guy multiple times and see if he dies once or if he dies over and over and over let's give it a go so we hit him he got hit he got hit again he doesn't look like he's dying more than once looks like he's just dying the one time yep everything i think is working pretty well with this guy now now we're going to get into levels and level progression so far we've been building a single level off of our sample scene the default level provided by unity and we're going to change that up we're going to set up a level one and a level two we're going to make it so that when we kill all of the monsters on a level we can progress to the next level whatever that is and make it so you can build multiple levels and start to put together an actual game so the first thing that we're going to do is re-save our scene instead of being named sample scene we're gonna save it as level one to do that i'm gonna go to file and choose save as we'll go to our scenes subfolder and i'll name this level one with capital l and just the number one at the end notice that we have sample seen in there already that's that default scene that it gave us so here i've just saved an exact copy of it if i go to my scenes folder and i double click on sample scene instead of switch to load that scene it looks exactly the same if i double click on level one it looks exactly the same as well because we haven't made any changes to it i'm gonna zoom out level one a little bit drag around pan with that right mouse button and i'm gonna take my monster here not the particle system here notice that when i clicked on it it selected the particle system that's not what i want i want the monster so i'm going to go over to the hierarchy select him and then i'm going to hit ctrl d and duplicate him so i get a copy of him i'll drag him up and over to the right if i had accidentally clicked on this and duplicated the particle system and drug it around i'd see something blank and i'd have a second particle system there and i need to go and click on it and delete that duplicate particle system so i've got two monsters i'm going to save my level and then i'm gonna open up sample scene and look at that here there's only one monster if i open up level two i have two monsters or sorry level one i have two monsters all right let's make a level two to make a level two i'm just going to take some of these crates in fact you know what i'm going to do i'm going to take all of these crates and we're going to select every single one of them select the first one hold shift select last one hit control d and duplicate them and then move them over here so now i've got two rows of crates i'm going to take this monster make sure that i select the monster not the particle system and drag him over as well and then i'm going to select this monster look at that every single time i go to click on a monster it selects the particle system that can get really frustrating luckily there's a little trick that we can use to make that not happen so let's go select our monster here in the hierarchy double click on our monster script scroll up to the top and we can add this special attribute and i want to show you this because there are a lot of attributes like this that you're going to learn throughout your unity times and you should just know that they exist and that they can make things a little bit easier for you if you understand what they are or where they are that they exist i guess so we're going to go right up to above our class right above line seven add a new line at a square brace and here the attribute that we want to add is selection base took me a second to remember it to add the selection base attribute with the closing bracket right at the end come back into unity and then what will happen is whenever we click on an item if we click on a monsters that mean let me click off of everything it'll actually try to select the item or the game object or i guess it's really the game object or transform that has that component with the selection based on it it's a way to make it so that it's easier to select the right thing when you're trying to click on something that you know has a big hierarchy there we go now i've got my two monsters and i want to save this as level two so i'm gonna hit file save as and name this level two but first i'm gonna go to my scenes folder and then type it in level two so now i have a level one and a level two let's go to scenes open up level one by double clicking open up level two by double clicking cool i've got two levels i'm ready to progress right let's go play level two and see what happens so i play it bam i kill a monster kill a monster they're both dead and obviously nothing actually changes i can't go from level two to level one or level one to level two because we haven't written any code for it we've saved our levels but now we need to actually write some code to manage what level we're on and set up some progression from level one to level two and so on to manage level progression we're going to create a level controller script a simple script that keeps track of how many monsters are alive in our current level and then progresses us to the next level once they're all dead so to do that we're going to go into our scripts folder right click hit create and choose c-sharp script we'll name this level controller with a capital l and a capital c we want to use that pascal case for our class names we'll open up the file and now we have this mono behavior class called level controller with a start and an update we're not going to use the start so i'm actually going to delete that we'll take lines 11 through 7 select them and just hit delete so what do we want to do in our level controller well the first thing that we want to do is find all of our monsters we want to keep track of all the monsters in our level and then we just want to keep track of how many of them are dead or if any of them are still alive now a good place to get a list or a collection of all the monsters in our level is in our on enable method so we're going to implement that now we'll just type on enable allow it to auto complete i'm gonna delete that private keyword one more time and then right here on line nine we're gonna add an underscore and monsters this is going to be an array or a collection of monsters it's essentially just a group of monsters in row where we can access them by id or index starting with zero so monster zero is the first monster monster one will be the second monster monster two will be the third monster and so on they just go in order starting with number zero so monsters is going to be assigned by setting it to equals then we're going to use this magic method that's built into the mono behavior called find object with the s you got to get the one with the s of type and then we add in the less than and we're going to put the word monster with the capital m to match our class name and open close parentheses and a semicolon now if this has the s and is correct you should see a tooltip like that that says it's going to return back a monster array or a monster with square braces that means that collection of monsters i need to generate that field though so i'm going to select monsters and hit alt enter and then hit enter to generate the monsters field so now we have a field or a variable that holds a collection of monsters i'm going to double click on private hit delete twice and get rid of that because again still don't need it all right so now we have our collection of monsters what do we want to do well we want to loop through all of them in our update and see if um any of them are alive if any of them are alive then hey um we're probably we're good to go we can keep keep going on for our level if they're all dead though well really i guess that's what we're to check for if they're all dead if they're all dead we want to go on to the next level so let's just say if monsters are all dead oops let's add in an open close parentheses then on the next line we'll say go to next level and then open close parenthesis now what we're doing here is just coding with intent we're writing out the names of these methods that we want to do that are named for what we want the code to do so what i'm going to do now is generate a monsters are all dead method and then generate a go to next level method then we'll implement them then our code will be hopefully easy to read and easy to understand let's hit alt enter generate the method for monsters are all dead then i'll hit alt enter on go to next level i went down one first and generate the method for go to next level okay so monsters are all dead is the first one that we want to implement so we're going to delete the line 29 code of throwing a not implemented exception we're just going to loop over all of the monsters and we're going to check to see if they are active i figure if they're inactive meaning we've turned them off then they're dead we're not actually turning them off yet but we'll do that next we'll make it so that we turn them off and then when we turn them all off that means that they're all dead so what we'll do is loop through the monsters using what's called a for each loop so let's say four each and here i can actually hit tab a couple times and let it auto-generate the code for me i'm going to scroll down a bit and look here it says for each var item in collection var is again that generic variable i can leave that alone i'm going to replace item with monster and just tab to go over to each one of these and if you mess up you and you have to just click through that's fine you can do it you just click onto them and just change them type the code in yourself but i like using the autocomplete so for each monster in underscore monsters i hit enter and now i'm into the bracket or the abrasive code here on line 31. so what does this mean it means that we're going to go over every monster or look at every monster in there we're going to get a reference to it called monster and then we're going to run some code probably affecting that monster or looking at that monster so what we want to check here is if a monster is still alive then our ro monster's dead is definitely false so we'll say if monster.gamer then return false because we're going to say hey we're turning them off if they're dead so if any one of them is active they're not all dead they'll go to the end and by default if none of them were active we means that they're all dead so we'll return true return true there we go so this will tell us if all of our monsters are dead once we fix that monster code we're gonna make that change in one second the next thing that we need to do is go to the next level now i don't want to implement going to next level until i know that monsters are all dead is working so what we're going to do now is add a debug log we're going to write something out to the console and see if it goes there so we're going to add in the wordsdebug.log go to level and then i'm going to add a space i'm going to add go to the end of the quotes and add a plus and then we're going to put level name or actually let's call it next level name with a lowercase n a capital l and a capital n and then we'll add a semicolon to the end go to next level name i'm going to hit alt enter and i'm going to generate a field for it now before i scroll up i'm going to delete both of these private keywords just to get rid of them and make everything match then we'll scroll up to our next level name i'm going to remove the private keyword but i'm just going to double click on and add the serialized field now i'm going to take this and move it above my monsters just to keep them in order it does not matter at all technically i just like to keep things in the same order so that it's nice and clean so i'm going to take this line cut it go up paste it add a space and clear that line up so it looks nice and even and nice and clean at least clean for me i'm gonna save that off go back into unity and we're going to set up a level controller first let's open up level one so here we are we're in level one we need to create a level controller game object so i'll go game object and we're going to create an empty game object this is just one that doesn't come with any other components no sprite renderer or camera or anything else on it we'll right click on the transform and just hit reset so that it's all zeroed out and then we'll hit add component and we're going to add our level controller so just start typing it in and you'll see that level controller popped up and i just hit enter to add it now i want to give it the next level name i'm on level one so i'm gonna put the word level 2 there and then i'm going to rename my game object from game object to level controller i'll save my scene i'm going to hit play and then i'm going to kill all the monsters and see what happens so let's see we drag our bird back monster dies let's see there we go one monster dead two monsters dead nothing's happened so far if i go to my console window look there's nothing here no messages no logs no errors nothing at all okay what's going on well our monsters still aren't dying we're not setting them to inactive so let's make that change now we'll go to our monster double click on the monster script and then down here when we die we're just going to change this code to set active false and re-enable that now if we run it like this we're actually going to run into an issue because if we die we're going to set our it has died to true we'll toggle our sprite start our particle and then instantly turn our game object off so we won't see our particle system play we won't see our dead sprite and it's just going to kind of disappear right away kind of back to the original behavior we had and we're going to lose all of our cool stuff so let's make a little change to this let's modify dye to be a co-routine like we did before in our bird so let's change this from void to ienumerator if i can spell it right make sure you don't get the i enumerable and then we'll add a yield here so after our setting our sprite let's add a yield return new weight for seconds there we go and i'll just wait for one second to add a closing brace and a semicolon or closing parenthesis in a semicolon save that off and scroll up look at this we don't have any build errors everything seems like it would work totally fine but if i run this my characters are not gonna die watch i'm gonna take my monster i'm actually gonna move him over here so he's nice and easy to hit i'm going to hit play and drag my bird and launch him at that monster look at that he didn't die anymore he didn't do anything what's the problem what's going on here let's look at the monster code one more time the issue i'm going to collapse this should die is that we cannot call an i enumerator method without using start co routine if it has ienumerator as the return type and we don't use start code routine it's not going to work so you need to add start co-routine around the die with the parentheses that will fix the issue and that's one of those things that catches a lot of people off guard because it doesn't give you an error there's no indication of why it's not working so it just doesn't work and you may or may not notice it right away let's try it again now i should be able to kill him wait a second and watch him die there we go he kind of poofs and disappears now the reason that i'm not seeing the particle system let's stop playing is that we're actually disappearing we're replaying the particle system and disappearing on the same frame so i'm going to take line 42 hold shift and home and delete and cut that move it up here and paste it in save that off now i should see the particle and then he'll disappear let's try it one more time hit play drag smack that guy he closes his eyes and resets looking good nothing in the console about going to level two yet so let's shoot our bird up at that guy oh did i get him maybe not okay let's try one more shot at him see if i can maybe knock him out i'm pretty bad at this let's just take him i'm gonna cheat i'm gonna drag him right over here look at that and then i'm gonna launch my bird at him bam there we go he died and now it wants to go to level two and see that every frame it wants to go to level two so the next thing that we're gonna do is implement go to level two all right now we're going to set up our go to next level and this is actually pretty easy we really just need to change one line of code and then make a little change in our game's project settings so we're going to replace our debug.log go to level and in fact let's leave it there and add one more line below it what we're going to add in is scene manager and notice that it says unityengine.scene management i'm going to zoom out my file and scroll up if i hit enter it's actually going to add a using unity engine. scene management line right there you need that for this to work make sure that you have that in there and that it gets added automatically or you type it in and it matches exactly with what you see there if you don't have that it's not going to work you're going to get an error so we'll say scene manager dot load scene how to open parentheses and then put in next level name with our underscore let's zoom that in a bit and then add in a semicolon at the end that is all we need to load a scene by name this will load the level by that level name assuming that we have a valid level name in there let's hit play though and see if it works and if it doesn't work why it doesn't work to do that i'm gonna first delete this second monster to make it really easy on myself so i only have to kill this one bad guy here we go i've got the one bad guy i'll drag and shoot and look at that he dies and it says go to level two and then it says scene level two could not be loaded because it has not been added to the build settings or the asset bundle has not been loaded what does that mean why is it saying that why is everything broken we'll stop playing and we'll make a quick fix it's actually really simple if we go to file and build settings and then drag our build settings window over here you see that right at the top we have a scenes and build section and by default it has our sample scene in it that's not what we want what we want to do is delete the sample scene and then go to our scenes folder leaving this up and then just drag in level one and level two scenes and build is a way for us to set what scenes go into a build for a specific platform or for a specific build that we're doing the first scene that's in there will be the one that's loaded and launched by default that's usually like a launcher scene or a menu scene or something in our case it's just going to be level one but for us to load any scene it has to be in the scenes and build otherwise it just gets excluded from the build those assets don't go into it if they're not used in a different level that's in it and it just kind of all gets stripped out and left out so we need to make sure that we add any scenes that we want to use to this scenes and build now i'm going to close this and i need to go to file and save project because saving our build settings only happens when we hit file save project if my computer blows up or something i'm going to lose those settings if i haven't saved all right now i should be able to hit play kill this guy and hopefully progress to level two let's try it out bam we're now on to level two but level two isn't gonna allow me to progress any further because i don't have a level controller or a level 3 to go on to so let's stop playing and make a couple more changes now before we add more to level 2 or extend out and add more levels i want to work on a little bit of cleanup i want to show you how we can do some better camera systems and how we can use prefabs to reuse some of the stuff that we've done and not have to rebuild components constantly every time we want to put something in a level so we're going to start with a new camera system and that's the cinemachine camera system to use it we need to go to our package manager go to window and then choose package manager and by default it will show the packages in project you'll have this little drop down here showing you all of the things that are in the project for you right now i have visual studio unity ui a couple other things jet brains writer it's a custom editor that i use and some of the other defaults there i want to change this from in project to unity registry this will show me all of the packages available the reason these packages exist are so that unity can split up bits of functionality of the engine and then separate it out so that if you only need to have 2d stuff in your game you don't necessarily need to include 3d things if you only want 3d things you don't need 2d stuff and as they add more and more functionality you don't need to pull all of it in every single time you want to build a game unless it applies so let's scroll down and choose the cinemachine option this is a camera controller for unity that's really powerful and just does a lot for us i'm going to hit the install button and allow it to set up the package for me it should just take a second it might take a minute if your internet connection is slower or your hard drive is slower but it shouldn't take too long then once it's done what we're going to be able to do is use this camera system to control the way that we view our game and view our player and to keep multiple things on the screen at the same time so now i'm going to close the package manager and we should have a cinemachine window up here if you don't you might have errors in the console make sure that you hit clear go clean those up and fix it and then come back so let's go to cinemachine and what we're going to do is create a new 2d camera so i hit create 2d camera that's going to give me a camera area right here with this little box you can kind of see it not exactly what we want though we want to add one other thing we want to add what's called a target group so we're going to right click and hit create empty and then we're going to add a component to this empty game object and this component is going to be a target group so i hit add component search for the word target and look there i have a cinema machine target group so now i have a target group object and a cmv cam so what i want to do next is rename this game object to target group just so that i know what it is when i'm looking here that it's a named target group instead of game object when i'm looking at this list and then we're going to add some targets to our target group so we're going to hit plus and hit plus again and it's going to allow me to add two targets these are things that i want my camera to follow the first thing that i wanted to follow is my red bird so i'm going to click and hold not release my red bird and drop it on as the first value there the first target then i'm going to take my monster and drop that on as the second one now i have two things set up as the target and position mode set to group center so it's going to try to keep them in the screen not quite yet though first i have to go over to my vcam and i have to tell it to follow this target group so i take my cmv cam selected grab my target group and drop it right into this follow section i know this seems like just a lot of weird dragging around and set up but what we've done here is add quite a bit of cool little magic let's hit play and see what happens so there we go look at that we've got our monster and adjust the bird and as i drag this around we can see that the monster and the bird stay in frame this isn't perfect though right that's not exactly what i wanted i wanted to show these guys but i wanted to show some more space around them so let's make a little bit of a modification to do that we go to our target group and we can just add in a radius around our characters like add in a radius around the bird look as i grab and just drag it to the right i just click here drag to the right as that value goes up we can see what that box is going to look like so i'm going to get this up to maybe like a two around the bird and i might go with something as big as maybe a one on the monster now i hit play again take a look at our game and things are starting to look a little bit better now let's stop playing and let's try moving things a little bit further away let's take our red bird and just drag him to the left a little bit hit play and look at how much our game will have changed that now we've got a much bigger field of view in a much bigger area i also have a little bit of area down below on the bottom that we're going to want to clean up but in general you can see that i can expand or drag out really really far and well launch my bird quite a bit further i'll need to adjust his launch power if i'm going to go exactly that far but it's something that i have access to so let's stop playing now and talk a little bit more about cameras when i hit play or let's just do it i'm going to hit play drag this down and look here when i hit play and drag it down notice the little blue area below the ground i'm going to let go of this guy and go select my camera notice this background color this matches exactly with what you were seeing there if i go to my scene view you can't see it because we don't draw a background but in our game view we do draw a background for our camera so let's expand it out so we can see the entire thing there and then adjust that background color so that it matches so i'm going to take my target group and i'll just expand out this radius while we're playing for a moment go select my main camera and i'm going to hit the little eyedropper here and then just go select a piece of the dark brown down here there we go now i've got some brown down below and i think that that is not perfect but quite a bit better now if i wanted i could just extend out a different background or an image down below there but i think just having the background be brown is probably going to be okay for me for now so i'll launch him over to the right and there he goes he shoots over and kills the guy i'm going to stop playing and make a couple more adjustments to my target group now see i think that a value of oh actually i think i'm going to leave those alone i'll leave them at two and one but i'll go back to my camera and i need to redo my setting there because when i did my setting i was in play mode so i lost my saved value there we go i'll go select that color again save my scene off hit play one last time and i think i'll be good to go with my positions there we go i can launch my bird go flying and hit some things now let's say i wanted two enemies on level one and i want my target group to follow him i can just duplicate my monster purple open drag him over here to the right that was just control d with him selected now i've got two monsters i just go to my target group hit plus again and add in that other monster i've got both of my monsters in the target group and they'll both be followed let's see what happens after i kill one of them there we go so that guy is there he dies everything's still good game still works totally fine look at that and i can kill that guy everything's looking good now we're gonna dive into a really important unity concept and that's prefabs we're going to set up what's called a prefab or a prefabricated object that has all of our settings on it and all of our data that we kind of combine by setting up components assigning our scripts and assigning our values we're going to put these into these objects and then reuse them across levels in our game we'll start with the bird and the monster and then we'll go on to the camera and the level controller start extending them all out so that we can reuse them across levels really really easily so let's start by making our bird into a prefab to do that we're going to start by making a prefab folder under our assets folder so select assets go to this empty area right click hit create folder and call this prefabs i'll go into that prefabs folder and i'm going to take my red bird i'm going to click don't release drag it down to here and let it go notice that it turned blue and i now have one down here this one is selected and it's showing up in the inspector it also has an open prefab button so right now i have this prefabricated object of a bird that i can make some changes to and if i make changes to this instance of of it down here in my project view it'll actually change it in this level as well let's see what that looks like i'm going to hit open prefab it's going to go into this custom editor mode and i'm just going to make a very small change to it i'm going to change the launch force to 1300 that's it then i'm going to hit the back button to exit out of the prefab and it's going to ask me if i want to save my changes to the prefab i'll hit save then go back to my bird and look at that the value is now 1300 okay pretty cool right so i can make changes to it down here and it applies there who cares right that's probably what you're wondering well let's go change this in level two now so i'm gonna open up level two go to scenes and double click on level two and yes we do want to save level one scene right now our redbird here is not blue he's gray and he still has that other launch force value so if i was building a game where i wanted to change all of the launch forces this wouldn't have worked i wouldn't have been able to update it here i would have to go into code and change it let's fix it though let's make this value or this red bird actually use the prefab and see what that looks like so i'm going to delete my red bird out of there go to my prefabs folder i'll take my red bird and just drop them in drag him around said he snaps into a good spot and look at his launch force it's at 1300 i'm going to save and now let's say that i'm playing with him right and i'm like oh 1300 seems too big that's too too strong of a number i think 1100 is actually the right number i'm here in level two like okay 1100 was good i think that i'm going to stick with that notice that i've changed it and it turned bold it's got this slightly different text it's kind of hard to tell actually but it is changed and you can see there's a little blue line here too indicating that this value is different from the prefab value i have a couple options here if i wanted i could always just right click or what is it right click right here on the overrides not right click and hit revert all to update it or go back to the default value so if i hit that it's going to go to 1300 another option say i set this to 1300 is that i might want to apply this to all of the birds in our game maybe i'm like hey 1100 is the right value and i could either go over to this red bird and maybe change it here and update the prefab or if i've made multiple changes i can also go to overrides and just over to the right if i drag this over you'll see it there's an apply all button and that'll make this not bold anymore and update the prefab down here as well so now that i've updated the prefab let's go see it in level one go to scenes and level one i'll save my level two scene look at level one the launch force is now eleven hundred now this doesn't just change for new var or variable changes like a float changing a value from a thousand to eleven hundred we could add components resize the thing add children to it re-change the hierarchy of it do a whole bunch of stuff to it right now we're just keeping it relatively simple so we've got our bird prefab all set up and ready to go so we can modify him across levels let's do the same for our monster so go to the prefabs folder i'm going to take monster purple open and actually i'm going to rename him i'm going to hit f2 to go to rename mode i'm just going to name him monster purple i think that's good enough or that's the name of purple monster our bird is red bird so purple monster makes sense i'll drag him down here and now he's a prefab i'll delete the other one and then i'll just take this other monster and duplicate him and drag him over now if i make changes or updates to this monster it'll update both of my monsters now i want to go into level two and do the same thing so open that up save level one go find my two monsters in fact i can just go delete them go into my prefabs folder and then take out the purple monster drop them in and maybe duplicate them and set up a couple of them here for my level so you can see how we can do this process for multiple objects and make things a little bit more reusable so we don't have to go reset them up or copy them across levels we can just use the one instance of it or the one reference or prefab of it and then place that anywhere that we want we could do the same for our crates set these up with box colliders and rigid bodies i don't think that i'm going to go through that process right now but you're welcome to try it out but we do want to go back through level one and do it for a couple other things so we'll go into level one and we wanna do the same thing for our level controller we wanna make that into a prefab so i'll go to prefabs turn that into a prefab by dragging it down there and i wanna do the same for my camera setup so my camera setup right now consists of this target group this cmv cam and this main camera object i want to set them all as children of a single camera object and make that a prefab to do that i'm going to create an empty game object go to game object create empty and reset the transform out to zero then i will take oh actually first i'll rename this to camera setup then i'll take the vcam drag it under as a child so just drag it and drop it right on top make it a child do the same for the target group drop it right on the camera group and it'll set or camera setup it'll drop there as a child and do the same for the main camera drop all three of those in they will all become children now i can take my camera set up and i can drop it down here as another prefab now i have a camera setup prefab a bird prefab and some monster prefabs save my scene with control s go over to level two and all i need to do to get my camera controller working and everything going is take my camera setup and drop it out here and then delete that other main camera there were two cameras there i don't want to have two there so i needed to delete the other one now i'll expand out my camera setup and set up my target group so every time i drop one of these camera setups in here i'm still going to need to modify the target group to match with my characters for the scene there are ways to do this programmatically but we're not going to get into those for this this tutorial at least so i'll take my red bird drop them in take the purple monster drop them in take the other purple monster drop them in and hey this target group needs another one so i'll take the other purple monster and drop him in one other little change i want to do though is move my monsters so that they're all next to each other there we go just rearrange them i could also set up a parent for them that's just a empty transformer an empty game object named monsters and make them all children of it as well same with the crates but i'm not going to do that right this moment i'll save this off and then let's drop a level controller in so we put a level controller in to level two notice that it's trying to go to level two as well we're already on level two so i'm just gonna make that go back to level one and save now i don't want to apply this prefab change because if i did it would change the one level one and to make it go to level one as well and i want that to go to level two so this one goes to level two or level one and the one in level one see if i go to scene one level one here and i select my level controller goes to level two let's try it out let's play see if i can go back and forth between the two levels killing my monsters oh let's stop playing because notice that this monster isn't in my target group so i'll expand out my camera setup expand out my select my target group and go assign that monster he's actually not there because i deleted and recreated him so he kind of fell off of there let's hit play and shoot my bird look at that killed one oh i almost got the other guy so close let's try again hopefully i can get him all right got him do i go into level two yep we're in level two let's see if i can get into level three i can kill all these guys and make it on one two or not really level three i'll go back to level one right there we go we're looping back to level one we now have control over it and hopefully you can see how you could just set up a bunch of different levels with different challenges and progress all the way through them now i wanted to end this tutorial or video with a little challenge so far i've shown you how to set up a bird how to set up a monster how to fly into things kill them and hopefully progress through levels what i'd like you to try is setting up a bird with the other art set up this meek green bird make him go flying maybe make him act a little bit different make a little bit of a change to him maybe to his speed or some of the rigid body settings or even try writing a little bit of code to solve a different idea maybe like a speed boost idea or something else that you have also if you have some just questions or things that you'd like to see added on to a follow-up about this thing let me know down below and if you really like the art and you really want to get into this and start extending out more and really get into game development check out the full art pack that's available the artist who made the art that's available for free here also made a bunch of other stuff that you can grab for really cheap and just build into a fun full game and help support the artist along the way anyway i wanted to say thanks again for following along with this tutorial i hope that you learned a lot and that you're kind of getting a hold on game development and starting to understand how to write code and don't give up i think that game development is a great option for anybody who's interested in it if you like video games you like building games or you had any kind of fun with it it just gets more and more fun as you get better at it as things get easier you can start just making ideas come true and make them appear on screen and it's a giant blast so i highly recommend it for anybody who's into it just keep at it keep trying and again if you run into problems have questions or issues along the way drop a comment down below i'm sure that either i or somebody else in the comments will have an answer for you be able to help you along the way also if you're interested in learning more make sure that you check out the link below i'll have some extensions and add-ons for this as well as some of my courses where i go a lot more in depth on game development and of course make sure that you're subscribed hit the thumbs up and share this video if you don't mind i really appreciate it again thanks everybody i hope you liked making the game i hope you take it far and have a great time with it goodbye
Info
Channel: Jason Weimann
Views: 399,318
Rating: undefined out of 5
Keywords: make a game, unity3d, dani, unity, game, games, brackeys, unity tutorial, c#, easy, coding, programming, computer science, unity2d, unity 2020, unity3d 2020, gamedev, game development, angry birds, unity 3d, how to make games in unity, unity 2d, make a game in unity, sykoo, jason weimann, unity3d.college, game dev, unity3d college, quill18, unity 2020.1, mkae a game, maek a game, make a game in unity3d
Id: Lu76c85LhGY
Channel Id: undefined
Length: 163min 13sec (9793 seconds)
Published: Wed Oct 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.